如何在框架中禁用导航快捷方式c# WPF

vom3gejh  于 2023-04-22  发布在  C#
关注(0)|答案(6)|浏览(161)

如何禁用框架中的导航快捷键(例如,“Backspace”用于向后导航,“Alt+向右箭头”用于向前导航)。
我想使用其他键盘功能,所以我想禁用框架的导航快捷方式。
谁能帮我?

zz2j4svz

zz2j4svz1#

有一个更优雅的解决方案,其中Attached behavior可以用于禁用导航,而无需实际扩展帧。
创建一个attached-behavior:

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的顶部添加import:

xmlns:beha="clr-namespace:A"
f87krz0w

f87krz0w2#

有关如何禁用键盘快捷键,请参阅以下答案:
Disable backspace in wpf
这对后退和前进导航鼠标按钮不起作用。为了防止这种情况,似乎需要在Navigating事件上放置一个处理程序,如果不需要,可以取消它。
例如,要完全禁用向前导航:
In .xaml:

<Frame Navigating="HandleNavigating" />

在后面的代码中:

void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Forward)
        {
            e.Cancel = true;
        }
    }
af7jpaap

af7jpaap3#

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

w7t8yxp5

w7t8yxp54#

在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) => { }));
}
jslywgbw

jslywgbw5#

框架本身没有提供禁用导航的方法。它只提供了一种隐藏导航控件的方法。但是,您可以从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" />
pn9klfpd

pn9klfpd6#

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;
        }
}

相关问题