Maui增加菜单快捷键

Maui增加菜单快捷键

  1. 全局快捷键
    CreateMauiApp方法中添加代码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    Microsoft.Maui.Handlers.PageHandler.Mapper.AppendToMapping(nameof(PageHandler), (handler, view) =>
                {
    #if WINDOWS
                    handler.PlatformView.PreviewKeyUp += (sender, e) =>
                    {
    
                    };
                    handler.PlatformView.PreviewKeyDown += (sender, e) =>
                    {
    
                    };
                    handler.PlatformView.KeyUp += (sender, e) =>
                    {
    
                    };
                    handler.PlatformView.KeyDown += (sender, e) =>
                    {
    
                    };
    #endif
                });
    

    如果需要使用空格等跟tab键切换相关的按键时,需要将控件上的Focus和tabstop属性设置成false,代码如下。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    ButtonHandler.Mapper.AppendToMapping("Project.Buttons.NoSystemFocus", (IButtonHandler handler, IButton view) =>
                {
                    if (view is Button)
                    {
                        handler.PlatformView.UseSystemFocusVisuals = false;
                        handler.PlatformView.IsTabStop = false;
                    }
                });
                ImageButtonHandler.Mapper.AppendToMapping("Project.ImageBButtons.NoSystemFocus", (handler, view) =>
                {
                    if (view is ImageButton)
                    {
                        handler.PlatformView.UseSystemFocusVisuals = false;
                        handler.PlatformView.IsTabStop = false;
                    }
                });
    

    如果使用CollectionView等列表形式的控件绑定数据的,禁用focus和tabstop需要在页面的后端代码中进行设置,前端传递控件对象给后端。

    1
    2
    3
    4
    
    <toolkit:EventToCommandBehavior                                                    
         EventName="ChildAdded"
         Command="{Binding CollectionViewChildAddedCommand}"
         CommandParameter="{Binding Source={x:Reference Listview}}"/>
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    [RelayCommand]
    void CollectionViewChildAdded(object sender)
    {
        if (sender != null && sender is CollectionView collectionView)
        {
            if (collectionView.Handler != null && collectionView.Handler is CollectionViewHandler viewHandler)
            {
                foreach (var item in viewHandler.PlatformView.ItemsPanelRoot.Children)
                {
                    item.IsTabStop = false;
                }
            }
        }
    }
    
  2. 页面快捷键
    在使用shell模式时,可以将导航条隐藏,利用菜单的快捷键实现页面上的快捷按键操作。

    1
    
    Shell.NavBarIsVisible="false"
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    <MenuFlyout>
        <MenuFlyoutItem Command="{Binding StartOrStopCommand}" IsEnabled="{Binding IsEnabledPage}">
            <MenuFlyoutItem.Text>
                <MultiBinding StringFormat="{}{0}/{1}">
                    <Binding Path="LocalizationResourceManager[Start]"/>
                    <Binding Path="LocalizationResourceManager[Stop]"/>
                </MultiBinding>
            </MenuFlyoutItem.Text>
            <MenuFlyoutItem.KeyboardAccelerators>
                <KeyboardAccelerator Modifiers="None" Key="Space" />
            </MenuFlyoutItem.KeyboardAccelerators>
        </MenuFlyoutItem>
        <MenuFlyoutItem Text="{Binding LocalizationResourceManager[Reset]}" Command="{Binding SpirometryResetCommand}" IsEnabled="{Binding SpirometryStartCommand.IsRunning, Mode=OneWay}">
            <MenuFlyoutItem.KeyboardAccelerators>
                <KeyboardAccelerator Modifiers="None" Key="R" />
            </MenuFlyoutItem.KeyboardAccelerators>
        </MenuFlyoutItem>
        <MenuFlyoutItem Text="{Binding LocalizationResourceManager[ReturnHome]}" Command="{Binding GoHomeCommand}" IsEnabled="{Binding SpirometryStartCommand.IsRunning, Mode=OneWay, Converter={StaticResource InvertedBoolConverter}}">
            <MenuFlyoutItem.KeyboardAccelerators>
                <KeyboardAccelerator Modifiers="None" Key="Escape" />
            </MenuFlyoutItem.KeyboardAccelerators>
        </MenuFlyoutItem>
    </MenuFlyout>
    
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy