如何禁用框架c#WPF中的导航快捷方式
本文关键字:导航 快捷方式 c#WPF 何禁用 框架 | 更新日期: 2023-09-27 17:59:12
如何禁用帧中的导航快捷方式(例如,"Backspace"用于向后导航,"Alt+Right arrow"用于向前导航)。
我想使用其他键盘功能,所以我想禁用框架的导航快捷键。
谁能帮我?
有一个更优雅的解决方案,可以使用附加行为来禁用导航,而无需实际扩展框架。
创建附加行为:
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"
关于如何禁用键盘快捷键,请参阅以下答案:
禁用wpf 中的退格
这对前后导航鼠标按钮不起作用。为了防止这种情况发生,您似乎需要在Navigation事件上设置一个处理程序,如果不需要,则取消它
例如,要完全禁用前向导航:
In.xaml:
<Frame Navigating="HandleNavigating" />
在代码背后:
void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Forward)
{
e.Cancel = true;
}
}
我所做的是在ContentControl中托管内容。
禁用WPF帧中所有快捷方式的真正答案是:
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;
}
}