【问题标题】:Windows 10 UAP back buttonWindows 10 UAP 后退按钮
【发布时间】:2015-06-02 13:29:59
【问题描述】:

如何处理 windows mobile 10 的后退按钮和 windows 10 平板电脑模式的后退按钮?我一直在到处寻找,但找不到任何例子。

【问题讨论】:

    标签: c# windows windows-10


    【解决方案1】:

    本主题是Guide to Universal Windows Platform apps 中使用的示例之一。我强烈建议在开始使用通用应用程序时阅读。

    对于页眉上的按钮,使用 Windows.UI.Core.SystemNavigationManager 并设置 AppViewBackButtonVisibility 属性以显示或隐藏按钮并处理 BackRequested 事件以执行导航。

    Windows.UI.Core.SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
    Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += (s,a) =>
    {
        Debug.WriteLine("BackRequested");
        if (Frame.CanGoBack)
        {
            Frame.GoBack();
            a.Handled = true;
        }
    }
    

    您连接硬件后退按钮的方式与在 Windows Phone 8.1 中的操作相同,但您应该检查 PhoneContract(或单独的类和方法)以确保它存在:

    if (ApiInformation.IsApiContractPresent ("Windows.Phone.PhoneContract", 1, 0)) {  
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += (s, a) =>
        {
            Debug.WriteLine("BackPressed");
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
                a.Handled = true;
            }
        };
    }
    

    【讨论】:

    • AppViewBackButtonVisibility 应该放在哪里? MainPage 构造函数对我和 App.xaml.cs 中的 OnLaunched 没有任何作用
    • 它会自动出现在标题栏上:)
    • 有没有办法自定义后退按钮的背景颜色?
    • 也许最好检查一下“ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))”,因为不是每部手机都能获得像 Lumia 630 等这样的 HardwareButtons。
    • 检查契约或类型在这里是等价的:如果契约存在,那么类必须存在。 Lumia 630 上的软返回按钮仍然来自 HardwareButtons 类。
    【解决方案2】:

    将以下代码添加到您的 App.xaml.cs 中,它将处理桌面、平板电脑和移动设备上的导航(我在移动模拟器上测试过) 为了更好地突出差异和解释(Handling The Back Button In Windows 10 UWP AppsJEFF PROSISE

    sealed partial class App : Application
    {
    
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }
    
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
    
            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
    
                rootFrame.NavigationFailed += OnNavigationFailed;
                rootFrame.Navigated += OnNavigated;
    
                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // TODO: Load state from previously suspended application
                }
    
                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
    
                // Register a handler for BackRequested events and set the
                // visibility of the Back button
                SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                    rootFrame.CanGoBack  ?
                    AppViewBackButtonVisibility.Visible :
                    AppViewBackButtonVisibility.Collapsed;
            }
    
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
    
            // Ensure the current window is active
            Window.Current.Activate();
        }
    
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }
    
        private void OnNavigated(object sender, NavigationEventArgs e)
        {
            // Each time a navigation event occurs, update the Back button's visibility
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                ((Frame)sender).CanGoBack ?
                AppViewBackButtonVisibility.Visible :
                AppViewBackButtonVisibility.Collapsed;
        }
    
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            // TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    
        private void OnBackRequested(object sender, BackRequestedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
    
            if (rootFrame.CanGoBack)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }
    }
    

    【讨论】:

    • 我正在我的 Lumia 950 上尝试这样做,以隐藏按钮、返回、主页和搜索的按钮。它什么也没做,它们仍然在那里,有什么想法吗?
    • 答案是关于出现在左上角的特殊后退按钮并处理具有退出应用程序默认行为的硬件按钮。它与主页和搜索按钮没有任何关系。
    • 啊,好的,谢谢,我的错。我最终找到了我想要的信息。
    • 我有一个错误,比如这张照片:1drv.ms/i/s!Auqiv8Ukng7U7Q0yE5XjtLC-RX8A。如何处理?
    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2016-02-05
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多