Windows 10 Frame.GoBack()方法中的奇怪问题?

时间:2016-05-30 05:10:41

标签: c# uwp windows-10-universal

我正在使用Windows 10,我正在使用硬件按钮在所有页面上导航,但在某些情况下,操作完成时。我正在使用this.Frame.GoBack()导航回上一页。但是当我使用这种方法导航回来时,当我第一次点击硬件后退按钮时没有任何反应。当我第二次点击它时。只有在使用this.Frame.GoBack()时才会发生这种情况。

第1页的示例代码

private void AddNotePage_Loaded(object sender, RoutedEventArgs e)
    {
        if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
        {
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }
        else
        {
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            SystemNavigationManager.GetForCurrentView().BackRequested += (s, e1) =>
            {
                if (this.Frame.CanGoBack)
                    this.Frame.GoBack();
            };
        }
    }

     protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
    }

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        Debug.WriteLine("BackPressed");
        if (Frame.CanGoBack)
        {
            Frame.GoBack();
            e.Handled = true;
        }
    }

第2页的示例代码

 private void AddNotePage_Loaded(object sender, RoutedEventArgs e)
    {
        if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
        {
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }
        else
        {
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            SystemNavigationManager.GetForCurrentView().BackRequested += (s, e1) =>
            {
                if (this.Frame.CanGoBack)
                    this.Frame.GoBack();
            };
        }
 }

 protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
    }

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        Debug.WriteLine("BackPressed");
        if (Frame.CanGoBack)
        {
            Frame.GoBack();
            e.Handled = true;
        }
    }

     private async void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
         // calling web service and when I get result=1 from it. I navigate  back
         this.Frame.GoBack();
    }

请有人建议我为什么要面对这个问题吗?

1 个答案:

答案 0 :(得分:1)

Page1.xaml   
 <Page
        x:Class="test1.BlankPage1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:test1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <SplitView x:Name="testSplitView" PaneBackground="White" PanePlacement="Right" OpenPaneLength="200">
        <SplitView.Pane>
            <Button Content="goto page 2" Click="Button_Click"/>
        </SplitView.Pane>

        <SplitView.Content>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Button x:Name="HamburgerButton"
                            FontFamily="Segoe MDL2 Assets" 
                            Content="&#xE700;"
                            Width="50"
                            Height="50" 
                            Background="Transparent"
                            Grid.Row="0"
                            HorizontalAlignment="Right"
                            VerticalAlignment="Center"
                            Click="HamburgerButton_Click"/>

                <Button Content="Don't click Just added for testing" Click="Button_Click"
                        Grid.Row="1"/>
            </Grid>
        </SplitView.Content>

    </SplitView>

</Grid>
    </Page>

Page1.xaml.cs
public sealed partial class BlankPage1: Page
    {
        public BlankPage1()
        {
            this.InitializeComponent();
            this.Loaded += BlankPage1_Loaded;
        }

        private void BlankPage1_Loaded(object sender, RoutedEventArgs e)
        {
            if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                SystemNavigationManager.GetForCurrentView().BackRequested += (s, e1) =>
                {
                    if (this.Frame.CanGoBack)
                        this.Frame.GoBack();
                };
            }

        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }
        private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
                e.Handled = true;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (this.testSplitView.IsPaneOpen)
                this.testSplitView.IsPaneOpen = false;
            Frame.Navigate(typeof(BlankPage2));
        }

         private void HamburgerButton_Click(object sender, RoutedEventArgs e)
    {
        this.testSplitView.IsPaneOpen = true;
    }
    }
page2.xaml

<Page
    x:Class="test1.BlankPage2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="goto page3" Click="Button_Click"/>
    </Grid>
</Page>
page2.xaml.cs

  public sealed partial class BlankPage2 : Page
    {
        public BlankPage2()
        {
            this.InitializeComponent();
            this.Loaded += BlankPage2_Loaded;
        }
        private void BlankPage2_Loaded(object sender, RoutedEventArgs e)
        {
            if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                SystemNavigationManager.GetForCurrentView().BackRequested += (s, e1) =>
                {
                    if (this.Frame.CanGoBack)
                        this.Frame.GoBack();
                };
            }
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }
        private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
                e.Handled = true;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(BlankPage3));
        }
}
page3.xaml
<Page
    x:Class="test1.BlankPage3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="goback to page2" Click="Button_Click"/>
    </Grid>
</Page>

page3.xaml.cs
 public sealed partial class BlankPage3 : Page
    {
        public BlankPage3()
        {
            this.InitializeComponent();
            this.Loaded += BlankPage3_Loaded;
        }

        private void BlankPage3_Loaded(object sender, RoutedEventArgs e)
        {
            if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                SystemNavigationManager.GetForCurrentView().BackRequested += (s, e1) =>
                {
                    if (this.Frame.CanGoBack)
                        this.Frame.GoBack();
                };
            }

        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }
        private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
                e.Handled = true;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
               Frame.GoBack();
            }
        }
    }