如何禁用框架中的导航快捷键(例如,“Backspace”用于向后导航,“Alt+右箭头”用于向前导航)。
我想使用其他键盘功能,因此我想禁用框架的导航快捷键。
谁可以帮助我?
如何禁用框架中的导航快捷键(例如,“Backspace”用于向后导航,“Alt+右箭头”用于向前导航)。
我想使用其他键盘功能,因此我想禁用框架的导航快捷键。
谁可以帮助我?
有一种更优雅的解决方案,可以使用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"
查看此答案以了解如何禁用键盘快捷键:
但这不能防止鼠标的前进和后退导航按钮。为了防止这种情况,似乎需要在“Navigating”事件上设置一个处理程序,并在不需要时取消它。
例如,要完全禁用前进导航:
在.xaml文件中:
<Frame Navigating="HandleNavigating" />
void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Forward)
{
e.Cancel = true;
}
}
禁用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) => { }));
}
框架本身没有提供禁用导航的方法。它只提供了隐藏导航控件的手段。但是,您可以从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" />
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;
}
}