如何在 C# WPF 中禁用框架导航快捷键

14

如何禁用框架中的导航快捷键(例如,“Backspace”用于向后导航,“Alt+右箭头”用于向前导航)。

我想使用其他键盘功能,因此我想禁用框架的导航快捷键。

谁可以帮助我?

6个回答

18

有一种更优雅的解决方案,可以使用Attached behaviours来禁用导航而不需要实际扩展框架。

创建一个attached-behaviour:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace A
{
    public static class DisableNavigation
    {
        public static bool GetDisable(DependencyObject o)
        {
            return (bool)o.GetValue(DisableProperty);
        }
        public static void SetDisable(DependencyObject o, bool value)
        {
            o.SetValue(DisableProperty, value);
        }

        public static readonly DependencyProperty DisableProperty =
            DependencyProperty.RegisterAttached("Disable", typeof(bool), typeof(DisableNavigation),
                                                new PropertyMetadata(false, DisableChanged));



        public static void DisableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var frame = (Frame)sender;
                       frame.Navigated += DontNavigate;
            frame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
        }

        public static void DontNavigate(object sender, NavigationEventArgs e)
        {
            ((Frame)sender).NavigationService.RemoveBackEntry();
        }
    }
}

并且在XAML中,每当使用帧时添加以下内容:

<Frame beha:DisableNavigation.Disable="True" />

在 XAML 文件的顶部添加以下引用:

xmlns:beha="clr-namespace:A"

5

查看此答案以了解如何禁用键盘快捷键:

禁用WPF中的退格键

但这不能防止鼠标的前进和后退导航按钮。为了防止这种情况,似乎需要在“Navigating”事件上设置一个处理程序,并在不需要时取消它。

例如,要完全禁用前进导航:

在.xaml文件中:

<Frame Navigating="HandleNavigating" />

在代码后台中:
    void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Forward)
        {
            e.Cancel = true;
        }
    }

1
这个简单的解决方案对我很有效。但是请添加代码以禁用后退导航: if(e.NavigationMode == NavigationMode.Back) { e.Cancel = true; } - Hallgeir Engen

1

禁用WPF Frame中的所有快捷键的真正方法是:

foreach (var vNavigationCommand in new RoutedUICommand[] 
                {   NavigationCommands.BrowseBack,
                    NavigationCommands.BrowseForward,
                    NavigationCommands.BrowseHome,
                    NavigationCommands.BrowseStop,
                    NavigationCommands.Refresh,
                    NavigationCommands.Favorites,
                    NavigationCommands.Search,
                    NavigationCommands.IncreaseZoom,
                    NavigationCommands.DecreaseZoom,
                    NavigationCommands.Zoom,
                    NavigationCommands.NextPage,
                    NavigationCommands.PreviousPage,
                    NavigationCommands.FirstPage,
                    NavigationCommands.LastPage,
                    NavigationCommands.GoToPage,
                    NavigationCommands.NavigateJournal })
{
    ctlFrame.CommandBindings.Add(new CommandBinding(vNavigationCommand, (sender, args) => { }));
}

1
我所做的是将内容托管在ContentControl中。

0

框架本身没有提供禁用导航的方法。它只提供了隐藏导航控件的手段。但是,您可以从Frame类继承并对其进行一些修改。以下示例在每次页面导航时都会从BackStack中删除最后一页。因此,确保框架无法向后导航,因为它不知道上一页是哪个。

class NoNavFrame : Frame
{
    public NoNavFrame()
    {
        this.Navigated += new System.Windows.Navigation.NavigatedEventHandler(NoNavFrame_Navigated);
    }

    void NoNavFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        this.NavigationService.RemoveBackEntry();
    }
}

然后您可以将其包含在XAML中,如下所示...

    <myControls:NoNavFrame x:Name="myFrame" NavigationUIVisibility="Hidden" />

0
Webview2 edge = new Webview2();

public Form1()
{
    InitializeComponent();
    edge.KeyDown += EdgeWebBrowser_KeyDown;
}

private void EdgeWebBrowser_KeyDown(object sender, KeyEventArgs e)
{
    if (((e.KeyCode == Keys.Left) && (GetAsyncKeyState(Keys.Menu) < 0)) || ((e.KeyCode == Keys.Right) && (GetAsyncKeyState(Keys.Menu) < 0)) || (e.KeyCode == Keys.F5) || ((e.KeyCode == Keys.R) && (GetAsyncKeyState(Keys.ControlKey) < 0)))
        {
            e.Handled = true;
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接