我正在使用prism作为我的Windows Phone 8.1运行时应用程序,我想在后台按下我的代码中编写几个条件,所以即使prism会处理硬件后退按钮流我需要在我的本地页面处理它(在cs页面或视图模型内部,所以我覆盖了我的app.cs页面中的OnHardwareButtonsBackPressed方法,在我的本地页面中,我将分别在onnavigatedto和onnavigatedfrom方法中注册和取消注册硬件backbutton事件并编写我的逻辑,它对我来说很好直到应用程序变为后台,如果我的应用程序进入后台并返回,则注册的硬件后退按钮事件将自动取消注册,从那时起回流不起作用。
//App.cs
public sealed partial class App : MvvmAppBase
{
public App()
{
this.InitializeComponent();
}
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
NavigationService.Navigate("Main", null);
return Task.FromResult<object>(null);
}
#if WINDOWS_PHONE_APP
protected async override void OnHardwareButtonsBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
var currentFrame = Window.Current.Content as Frame;
var currentView = currentFrame.Content as IView;
if (currentView != null)
{
var backNavigationVM = currentView.DataContext as IBackNavigation;
if ((backNavigationVM == null || backNavigationVM.CanGoBack))
{
base.OnHardwareButtonsBackPressed(sender, e);
}
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
#endif
}
//My page.xaml
<storeApps:VisualStateAwarePage
x:Class="BackButtonWithPrism.Views.SecondPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BackButtonWithPrism.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
xmlns:storeApps="using:Microsoft.Practices.Prism.StoreApps"
xmlns:mvvm="using:Microsoft.Practices.Prism.Mvvm"
mvvm:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<TextBlock Text="SecondPage" FontSize="{StaticResource TextStyleLargeFontSize}" HorizontalAlignment="Center"></TextBlock>
<Grid x:Name="FirstGrid">
<Button HorizontalAlignment="Center" Content="FirstGrid" Click="Button_Click"></Button>
</Grid>
<Grid x:Name="SecondGrid">
<Button HorizontalAlignment="Center" x:Name="btnSecond" Content="SecondGrid"></Button>
</Grid>
</Grid>
</storeApps:VisualStateAwarePage>
//My page.cs
public sealed partial class SecondPage : VisualStateAwarePage
{
public SecondPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
FirstGrid.Visibility = Visibility.Visible;
SecondGrid.Visibility = Visibility.Collapsed;
#if WINDOWS_PHONE_APP
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#endif
}
#if WINDOWS_PHONE_APP
private async void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (FirstGrid.Visibility == Visibility.Collapsed)
{
e.Handled = true;
FirstGrid.Visibility = Visibility.Visible;
SecondGrid.Visibility = Visibility.Collapsed;
}
else
{
Frame currentFrame = Window.Current.Content as Frame;
if (currentFrame == null)
{
return;
}
else if (currentFrame.CanGoBack)
{
currentFrame.GoBack();
e.Handled = true;
}
}
}
#endif
protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FirstGrid.Visibility = Visibility.Collapsed;
SecondGrid.Visibility = Visibility.Visible;
}
}
//My Page ViewModel:
public class SecondPageViewModel : ViewModel, IBackNavigation
{
public bool CanGoBack
{
get;
set;
}
}
这里我的所有逻辑都写在我的cs页面而不是在viewmodel中写入但是我以后会这样做。
答案 0 :(得分:0)
您的问题与OnNavigatedFrom()
实施有关。当应用程序被停用并进入后台时,将调用此方法,从而导致后退按钮处理程序的注销。不幸的是,当激活应用程序时,不会调用OnNavigatedTo()
来配对它。相反应该处理另一个事件 - Window.Activated。如果您不想将代码复杂化很多,只需尝试将后续处理程序包裹移除以下条件。当应用程序转到后台时,会传递Forward
具有当前页面类型的请求。
if (e.NavigationMode != NavigationMode.Forward || e.SourcePageType != GetType())
{
HardwareButtons.BackPressed -= ...
}
最后一个提示。当你在IDE中放置一个断点并启动调试器应用程序永远不会被挂起,永远不会收到相应的事件。要模拟它们(即激活,暂停和终止),请检查here或here。