【发布时间】:2017-03-03 20:37:25
【问题描述】:
在 Windows 8.1 通用应用程序中,暂停/恢复模式是使用 APP 模板中包含的 NavigationHelper.cs 和 SuspensionManager.cs 类处理的。 Windows 10 UWP 应用程序中似乎没有这些类。有没有办法可以处理挂起/恢复状态?
【问题讨论】:
标签: c# windows-store-apps windows-8.1 windows-10
在 Windows 8.1 通用应用程序中,暂停/恢复模式是使用 APP 模板中包含的 NavigationHelper.cs 和 SuspensionManager.cs 类处理的。 Windows 10 UWP 应用程序中似乎没有这些类。有没有办法可以处理挂起/恢复状态?
【问题讨论】:
标签: c# windows-store-apps windows-8.1 windows-10
社区正在开发一个有趣的框架(但我认为主要是 Jerry Nixon, Andy Wigley 等),称为 Template10。 Template10 有一个 Bootstrapper 类,其中包含您可以覆盖的 OnSuspending 和 OnResuming 虚拟方法。我不确定是否有使用 Template10 进行暂停/恢复的确切示例,但这个想法似乎是制作 App.xaml.cs inherit from this Bootstrapper 类,以便您可以轻松覆盖我提到的方法。
sealed partial class App : Common.BootStrapper
{
public App()
{
InitializeComponent();
this.SplashFactory = (e) => null;
}
public override Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
// start the user experience
NavigationService.Navigate(typeof(Views.MainPage), "123");
return Task.FromResult<object>(null);
}
public override Task OnSuspendingAsync(object s, SuspendingEventArgs e)
{
// handle suspending
}
public override void OnResuming(object s, object e)
{
// handle resuming
}
}
【讨论】:
上述解决方案仅适用于安装 Template10 的人。 通用解决方案是,
将这些行粘贴到 App.xaml.cs 的构造函数中
this.LeavingBackground += App_LeavingBackground;
this.Resuming += App_Resuming;
看起来像这样
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.LeavingBackground += App_LeavingBackground;
this.Resuming += App_Resuming;
}
这些是方法,尽管您可以按 TAB 键,它们会自动生成。
private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
{
}
private void App_Resuming(object sender, object e)
{
}
uwp中新增了LeavingBackground和这里没有提到的EnteredBackground方法。
在这些方法之前,我们会使用恢复和暂停来保存和恢复 ui,但现在推荐的地方是这里。这些也是应用程序恢复之前执行工作的最后一个地方。因此,这些方法的工作应该是小的 ui 或其他东西,比如重新制作陈旧的值,因为这里长期持有的方法会影响应用程序在恢复时的启动时间。
来源 Windows dev material , Windoes dev material 2
谢谢,祝你有美好的一天。
【讨论】: