none
UWP应用使用splitView在SplitView.Pane中添加按钮导航到其他页面丢失Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested事件 RRS feed

  • 问题

  • MainPage代码:

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <SplitView IsPaneOpen="True" OpenPaneLength="100" DisplayMode="Overlay">
                <SplitView.Pane>
                    <Button Click="Button_Click">Setting</Button>
                </SplitView.Pane>
            </SplitView>
        </Grid>

    MainPage的.cs

     public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Frame.Navigate(typeof(SettingPage));
            }
        }

    SettingPage的xaml没有内容,.cs文件内容为:

    public sealed partial class SettingPage : Page
        {
            public SettingPage()
            {
                this.InitializeComponent();
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += SettingPage_BackRequested;
                base.OnNavigatedTo(e);
            }
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested -= SettingPage_BackRequested;
                base.OnNavigatedFrom(e);
            }
    
            private void SettingPage_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
            {
                e.Handled = true;
                Frame.Navigate(typeof(MainPage));
            }
        }

    我想实现点击手机上的后退按钮时从settingPage页面返回到MainPage页面,但是在setting这个页面需要点击两次后退按钮才会触发一次BackRequested时间,也就是说丢失了一次。只有在splitView.Panel中的按钮事件中navigate到其他页面并且splitView的DisplayMode="Overlay"时才会出现丢失一次事件,是bug吗?有解决方案吗?

    2016年2月19日 11:40

答案

  • 您好,

    在UWP中处理后退按钮的事件不需要在每个页面都添加这样的处理方法,你只需要在App.xaml.cs中添加全局的注册事件就可以了。

    下面是我的代码,供你参考:

            protected override void OnLaunched(LaunchActivatedEventArgs e)
            {
    
    #if DEBUG
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    this.DebugSettings.EnableFrameRateCounter = true;
                }
    #endif
    
                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 += RootFrame_Navigated;
    
                    if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                    {
                        //TODO: Load state from previously suspended application
                    }
    
                    // Place the frame in the current Window
                    Window.Current.Content = rootFrame;
    
                    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();
            }
            private void RootFrame_Navigated(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 OnBackRequested(object sender, BackRequestedEventArgs e)
            {
                Frame rootFrame = Window.Current.Content as Frame;
    
                if (rootFrame.CanGoBack)
                {
                    e.Handled = true;
                    if (rootFrame.CurrentSourcePageType == typeof(Setting))
                    {
                        
                        rootFrame.Navigate(typeof(MainPage));
                    }
                    else
                    {
                        rootFrame.GoBack();
                    }
                }
            }



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年2月24日 3:29
    版主

全部回复

  • 您好,

    在UWP中处理后退按钮的事件不需要在每个页面都添加这样的处理方法,你只需要在App.xaml.cs中添加全局的注册事件就可以了。

    下面是我的代码,供你参考:

            protected override void OnLaunched(LaunchActivatedEventArgs e)
            {
    
    #if DEBUG
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    this.DebugSettings.EnableFrameRateCounter = true;
                }
    #endif
    
                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 += RootFrame_Navigated;
    
                    if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                    {
                        //TODO: Load state from previously suspended application
                    }
    
                    // Place the frame in the current Window
                    Window.Current.Content = rootFrame;
    
                    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();
            }
            private void RootFrame_Navigated(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 OnBackRequested(object sender, BackRequestedEventArgs e)
            {
                Frame rootFrame = Window.Current.Content as Frame;
    
                if (rootFrame.CanGoBack)
                {
                    e.Handled = true;
                    if (rootFrame.CurrentSourcePageType == typeof(Setting))
                    {
                        
                        rootFrame.Navigate(typeof(MainPage));
                    }
                    else
                    {
                        rootFrame.GoBack();
                    }
                }
            }



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年2月24日 3:29
    版主
  • 明白,谢谢。
    2016年3月4日 9:37