首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 10 UAP后退按钮

Windows 10 UAP后退按钮
EN

Stack Overflow用户
提问于 2015-06-02 21:30:00
回答 2查看 21.1K关注 0票数 21

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-03 06:31:43

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

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

代码语言:javascript
复制
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 phone8.1中一样连接硬件back按钮,但您应该检查PhoneContract (或单个类和方法)以确保它在那里:

代码语言:javascript
复制
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;
        }
    };
}
票数 30
EN

Stack Overflow用户

发布于 2015-12-14 00:13:47

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

代码语言:javascript
复制
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();
        }
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30597585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档