最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mvvm - how to handle keyboard event in maui with CommunityToolkit - Stack Overflow

programmeradmin1浏览0评论

I am coding an app in window and mac platform.I have finished code like this:

<Button x:Name="ForwardButton" Text="&gt;" Command="{Binding ForwardCommand}">

and it works well.But The button is an clcik event.The user will click the button many times.So I want to change the way to an keyboard event.The right button in keyboard will be the better way to use the function. Now I do not know how to let maui with CommunityToolkit catch the keyboard event. someone show me the code like this:

<ContentPage xmlns:mvvm="clr-namespace:CommunityToolkit.Mvvm.ComponentModel;assembly=CommunityToolkit.Mvvm" ...>
    ...
    <ContentPage.Behaviors>
        <mvvm:EventToCommandBehavior EventName="KeyUp" SourceObject="{x:Reference Name=myTextBox}" Command="{Binding EnterKeyPressedCommand}"/>
    </ContentPage.Behaviors>
</ContentPage>

but unfortunately,it does not work. So Could anyone tell me what to do? I have been blocked by the problem for a long time. thank you.

I am coding an app in window and mac platform.I have finished code like this:

<Button x:Name="ForwardButton" Text="&gt;" Command="{Binding ForwardCommand}">

and it works well.But The button is an clcik event.The user will click the button many times.So I want to change the way to an keyboard event.The right button in keyboard will be the better way to use the function. Now I do not know how to let maui with CommunityToolkit catch the keyboard event. someone show me the code like this:

<ContentPage xmlns:mvvm="clr-namespace:CommunityToolkit.Mvvm.ComponentModel;assembly=CommunityToolkit.Mvvm" ...>
    ...
    <ContentPage.Behaviors>
        <mvvm:EventToCommandBehavior EventName="KeyUp" SourceObject="{x:Reference Name=myTextBox}" Command="{Binding EnterKeyPressedCommand}"/>
    </ContentPage.Behaviors>
</ContentPage>

but unfortunately,it does not work. So Could anyone tell me what to do? I have been blocked by the problem for a long time. thank you.

Share asked Mar 3 at 19:49 jie xiaojie xiao 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I have found a perfect solution from msdn document. Now you can code like this:

First, create a label and use this code:

            <Label x:Name="MenuLabel" StyleClass="LabelStyle1" Text="RightClickMenu">
                <FlyoutBase.ContextFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="Back" Command="{Binding BackCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Left" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem Text="Forward" Command="{Binding ForwardCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Right" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem Text="GoToStart" Command="{Binding GoToStartCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Up" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem Text="GoToEnd" Command="{Binding GoToEndCommand}" >
                            <MenuFlyoutItem.KeyboardAccelerators>
                                <KeyboardAccelerator Modifiers="None" Key="Down" />
                            </MenuFlyoutItem.KeyboardAccelerators>
                        </MenuFlyoutItem>
                    </MenuFlyout>
                </FlyoutBase.ContextFlyout>
            </Label>

Second, hide the label. Now , You have keyboard event. It is so simple.

Attention!! the method is only available in core 8.

Good luck.

if you want to know more, you would read

https://learn.microsoft/en-us/dotnet/maui/user-interface/keyboard-accelerators?view=net-maui-8.0

thank you!

There is no api can detect keyup event in the maui. So you can use the platform native api to do that.

  • For the Windows

Put the following code in the ContenPage.cs:

        protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();
#if WINDOWS
            var winview = this.Handler.PlatformView as Microsoft.Maui.Platform.ContentPanel;
            winview.PreviewKeyUp += (s, e) =>
            {
                if(e.Key == Windows.System.VirtualKey.Enter)
                {
                    (((BindingContext as TargetViewModel).EnterCommand) as RelayCommand).Execute(null);
                    //excute the command
                }
            };
#endif
        }
  • For the MacCatalyst

Put the following code in the \Platforms\MacCatalyst\AppDelegate.cs

public override void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
 {
     if(App.Current.MainPage == TargetPage)
     {
         foreach (UIPress p in presses)
         {
             var key = p.Key;
             if (key.Characters == "\r")
             {
                 ((((App.Current.MainPage as TargetPage).BindingContext as TargetViewModel).EnterCommand) as RelayCommand).Execute(null);
             }
         }
     }
     
     base.PressesBegan(presses, evt);
 }
发布评论

评论列表(0)

  1. 暂无评论