在 windows 10 UWP 中处理挂起、恢复和激活
Handling suspend, resume, and activation in windows 10 UWP
在 windows 8.1 通用应用程序中,suspend/resume 模式是使用应用程序模板中包含的 NavigationHelper.cs
ans SuspensionManager.cs
类 处理的。这些 类 似乎不存在于 windows 10 个 UWP 应用程序中。有没有办法处理 suspend/resume 状态?
社区正在开发一个有趣的框架(但我认为主要是 Jerry Nixon, Andy Wigley etc.) called Template10. Template10 has a Bootstrapper class with OnSuspending
and OnResuming
virtual methods that you can override. I am not sure that there's an exact example of doing suspension/resuming yet with Template10, but the idea seems to be to make App.xaml.cs inherit from this Bootstrapper class 因此您可以轻松覆盖我提到的方法。
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,但现在推荐的执行该工作的位置是 here.Also 这些是在应用程序之前执行工作的最后位置恢复。因此,这些方法的工作应该很小 ui 或其他东西,例如重新制作陈旧的值,因为这里长期持有的方法会在恢复时影响应用程序启动时间。
来源
Windows dev material ,
Windoes dev material 2
谢谢,祝你有美好的一天。
在 windows 8.1 通用应用程序中,suspend/resume 模式是使用应用程序模板中包含的 NavigationHelper.cs
ans SuspensionManager.cs
类 处理的。这些 类 似乎不存在于 windows 10 个 UWP 应用程序中。有没有办法处理 suspend/resume 状态?
社区正在开发一个有趣的框架(但我认为主要是 Jerry Nixon, Andy Wigley etc.) called Template10. Template10 has a Bootstrapper class with OnSuspending
and OnResuming
virtual methods that you can override. I am not sure that there's an exact example of doing suspension/resuming yet with Template10, but the idea seems to be to make App.xaml.cs inherit from this Bootstrapper class 因此您可以轻松覆盖我提到的方法。
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,但现在推荐的执行该工作的位置是 here.Also 这些是在应用程序之前执行工作的最后位置恢复。因此,这些方法的工作应该很小 ui 或其他东西,例如重新制作陈旧的值,因为这里长期持有的方法会在恢复时影响应用程序启动时间。
来源 Windows dev material , Windoes dev material 2
谢谢,祝你有美好的一天。