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

发布于 2024-11-15 22:51:22 字数 106 浏览 5 评论 0原文

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

我想使用其他键盘功能,所以我想禁用框架的导航快捷方式。

谁能帮助我?

How can I disable the navigation shortcuts in a frame (for example the "Backspace" for navigation backward and "Alt+Right arrow" for navigation forward).

I want to use other keyboard functions, so I want to disable the navigation shortcuts of the frame.

Who can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只有影子陪我不离不弃 2024-11-22 22:51:22

有一个更优雅的解决方案,可以使用附加行为来禁用导航,而无需实际扩展框架。

创建一个附加行为:

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"

there is a more elegant solution where Attached behaviours can be used to disable navigation without actually extending a frame.

create an 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();
        }
    }
}

And in the xaml add this whenever you use a frame :

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

and at the top of the xaml add the import :

xmlns:beha="clr-namespace:A"
笑咖 2024-11-22 22:51:22

有关如何禁用键盘快捷键的信息,请参阅此答案:

Disable backspace in wpf

这不起作用用于后退和前进导航鼠标按钮。为了防止这种情况发生,您似乎需要在导航事件上放置一个处理程序,如果您不需要,则将其取消。

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

在 .xaml 中:

<Frame Navigating="HandleNavigating" />

在后面的代码中:

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

See this answer for how to disable the keyboard shortcuts:

Disable backspace in wpf

That doesn't work for the back and forward navigation mouse buttons. To prevent that, it seems you need to put a handler on the Navigating event and cancel it if you don't want it.

For example, to totally disable forward navigation:

In .xaml:

<Frame Navigating="HandleNavigating" />

In code behind:

    void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Forward)
        {
            e.Cancel = true;
        }
    }
冷情 2024-11-22 22:51:22

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

What I do is host the content in ContentControl.

凉城已无爱 2024-11-22 22:51:22

禁用 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) => { }));
}

The real answer to disable all shortcuts in WPF Frame is:

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) => { }));
}
触ぅ动初心 2024-11-22 22:51:22

它本身的框架没有提供禁用导航的方法。它仅提供隐藏导航控件的方法。不过,您可以继承 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" />

The frame it's self provides no method of disabling navigation. It only provides a means to hide the navigation controls. You can however inherit from the Frame class and make some modifications to it yourself. The following example removes the last page from the BackStack every time the page navigates. Thus ensuring the frame can never navigate backwards as it does not know which page was last.

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

Then you can include this in XAML as follows...

    <myControls:NoNavFrame x:Name="myFrame" NavigationUIVisibility="Hidden" />
‖放下 2024-11-22 22:51:22
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;
        }
}
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;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文