C# Windows 10 Frame.GoBack()方法中的奇怪问题?
我在Windows 10上工作,我使用硬件按钮导航回所有页面,但在某些情况下,当操作完成时。我正在使用C# Windows 10 Frame.GoBack()方法中的奇怪问题?,c#,uwp,windows-10-universal,C#,Uwp,Windows 10 Universal,我在Windows 10上工作,我使用硬件按钮导航回所有页面,但在某些情况下,当操作完成时。我正在使用this.Frame.GoBack()导航回上一页。但当我使用这种方法返回时,当我第一次单击硬件返回按钮时,什么也没发生。当我第二次单击它时,它会工作。仅当使用了This.Frame.GoBack()时才会发生这种情况 第1页的示例代码 private void AddNotePage_Loaded(object sender, RoutedEventArgs e) {
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();
}
有人能告诉我为什么要面对这个问题吗?Page1.xaml
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=""
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();
}
}
}
Page1.xaml.cs
公共密封部分类空白第1页:第
{
公共网页1()
{
this.InitializeComponent();
this.Loaded+=BlankPage1\u Loaded;
}
已加载专用无效空白页1_(对象发送方,路由目标e)
{
if(ApiInformation.IsTypePresent(“Windows.Phone.UI.Input.HardwareButtons”))
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed+=硬件按钮\u BackPressed;
}
其他的
{
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility=AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested+=(s,e1)=>
{
if(本帧CanGoBack)
this.Frame.GoBack();
};
}
}
受保护的覆盖无效OnNavigatedFrom(NavigationEventArgs e)
{
基于(e)的导航;
Windows.Phone.UI.Input.HardwareButtons.BackPressed-=HardwareButtons\u BackPressed;
}
私有无效硬件按钮\u反压(对象发送器,反压EventArgs e)
{
if(框架CanGoBack)
{
Frame.GoBack();
e、 已处理=正确;
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
if(this.testSplitView.IsPaneOpen)
this.testSplitView.IsPaneOpen=false;
框架导航(typeof(BlankPage2));
}
私有无效汉堡按钮点击(对象发送者,路由目标e)
{
this.testSplitView.IsPaneOpen=true;
}
}
第2.xaml页
page2.xaml.cs
公共密封部分类空白第2页:第
{
公共网页2()
{
this.InitializeComponent();
this.Loaded+=BlankPage2_Loaded;
}
已加载专用无效空白页2_(对象发送方,路由目标e)
{
if(ApiInformation.IsTypePresent(“Windows.Phone.UI.Input.HardwareButtons”))
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed+=硬件按钮\u BackPressed;
}
其他的
{
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility=AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested+=(s,e1)=>
{
if(本帧CanGoBack)
this.Frame.GoBack();
};
}
}
受保护的覆盖无效OnNavigatedFrom(NavigationEventArgs e)
{
基于(e)的导航;
Windows.Phone.UI.Input.HardwareButtons.BackPressed-=HardwareButtons\u BackPressed;
}
私有无效硬件按钮(对象发送器,Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if(框架CanGoBack)
{
Frame.GoBack();
e、 已处理=正确;
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
框架导航(typeof(BlankPage3));
}
}
第3.xaml页
page3.xaml.cs
公共密封部分类空白第3页:第
{
公共网页3()
{
this.InitializeComponent();
this.Loaded+=BlankPage3_Loaded;
}
已加载专用无效空白页3_(对象发送方、路由目标)
{
if(ApiInformation.IsTypePresent(“Windows.Phone.UI.Input.HardwareButtons”))
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed+=硬件按钮\u BackPressed;
}
其他的
{
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility=AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested+=(s,e1)=>
{
if(本帧CanGoBack)
this.Frame.GoBack();
};
}
}
受保护的覆盖无效OnNavigatedFrom(NavigationEventArgs e)
{
基于(e)的导航;
Windows.Phone.UI.Input.HardwareButtons.BackPressed-=HardwareButtons\u BackPressed;
}
私有无效硬件按钮\u反压(对象发送器,反压EventArgs e)
{
if(框架CanGoBack)
{
Frame.GoBack();
e、 已处理=正确;
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
if(框架CanGoBack)
{
Frame.GoBack();
}
}
}
您是否尝试过调试?第一次是Frame.CanGoBack=true
吗?是的,我尝试过调试,但当我第一次单击后退按钮时,它没有触发HardwareButtons\u BackPressed
事件。当我第二次点击它时,它会开火。硬件也可能会开火