简体   繁体   English

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

[英]How disable navigation shortcuts in frame c# WPF

How can I disable the navigation shortcuts in a frame (for example the "Backspace" for navigation backward and "Alt+Right arrow" for navigation forward). 如何禁用框架中的导航快捷方式(例如,“Backspace”用于向后导航,“Alt +向右箭头”用于向前导航)。

I want to use other keyboard functions, so I want to disable the navigation shortcuts of the frame. 我想使用其他键盘功能,所以我想禁用框架的导航快捷方式。

Who can help me? 谁能帮我?

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 : 在xaml中,每当你使用一个框架时添加它:

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

and at the top of the xaml add the import : 并在xaml的顶部添加导入:

xmlns:beha="clr-namespace:A"

See this answer for how to disable the keyboard shortcuts: 有关如何禁用键盘快捷键,请参阅此答案:

Disable backspace in wpf 禁用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. 为了防止这种情况,你似乎需要在Navigating事件上放置一个处理程序,如果你不想要它就取消它。

For example, to totally disable forward navigation: 例如,要完全禁用前向导航:

In .xaml: 在.xaml中:

<Frame Navigating="HandleNavigating" />

In code behind: 代码背后:

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

The real answer to disable all shortcuts in WPF Frame is: 禁用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) => { }));
}

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. 但是,您可以继承Frame类并自行对其进行一些修改。 The following example removes the last page from the BackStack every time the page navigates. 以下示例在每次页面导航时从BackStack中删除最后一页。 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... 然后您可以将此包含在XAML中,如下所示......

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM