重要的应用程序接口(API)
了解系统恢复应用时何时刷新用户界面。 此主题中的示例为 恢复 事件注册了事件处理程序。
注册恢复事件处理程序
注册以处理 恢复 事件,该事件表示用户已离开你的应用,然后返回到应用。
partial class MainPage
{
public MainPage()
{
InitializeComponent();
Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
}
}
Public NonInheritable Class MainPage
Public Sub New()
InitializeComponent()
AddHandler Application.Current.Resuming, AddressOf App_Resuming
End Sub
End Class
MainPage::MainPage()
{
InitializeComponent();
Windows::UI::Xaml::Application::Current().Resuming({ this, &MainPage::App_Resuming });
}
MainPage::MainPage()
{
InitializeComponent();
Application::Current->Resuming +=
ref new EventHandler<Platform::Object^>(this, &MainPage::App_Resuming);
}
刷新显示的内容和重新获取资源
用户切换到另一个应用或桌面后,系统会暂停应用几秒钟。 当用户切换回应用时,系统会恢复你的应用。 当系统恢复应用时,变量和数据结构的内容与系统暂停应用之前的内容相同。 系统将应用恢复到退出时的状态。 对用户来说,它看起来好像应用已在后台运行。
当应用处理 恢复 事件时,应用可能已暂停数小时或数天。 它应刷新应用暂停时可能已过时的任何内容,例如新闻源或用户的位置。
这也是还原应用暂停时释放的任何独占资源的好时机,例如文件句柄、相机、I/O 设备、外部设备和网络资源。
partial class MainPage
{
private void App_Resuming(Object sender, Object e)
{
// TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
}
}
Public NonInheritable Class MainPage
Private Sub App_Resuming(sender As Object, e As Object)
' TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
End Sub
>
End Class
void MainPage::App_Resuming(
Windows::Foundation::IInspectable const& /* sender */,
Windows::Foundation::IInspectable const& /* e */)
{
// TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
}
void MainPage::App_Resuming(Object^ sender, Object^ e)
{
// TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
}
注释
由于 恢复 事件未从 UI 线程引发,因此必须在处理程序中使用调度程序来调度对 UI 的任何调用。
注解
当应用附加到 Visual Studio 调试器时,它将不会挂起。 但是,你可以从调试器中暂停它,然后向其发送 Resume 事件,以便可以调试代码。 请确保 调试位置工具栏 可见,然后点击 暂停 图标旁边的下拉列表。 然后选择恢复。
对于 Windows Phone 应用商店应用,恢复 事件始终跟随 OnLaunched事件,即便应用当前已暂停,用户从主磁贴或应用列表中重新启动应用时也会如此。 如果当前窗口上已设置内容,应用可以跳过初始化。 可以检查 LaunchActivatedEventArgs.TileId 属性,以确定应用是从主磁贴或辅磁贴启动,并基于该信息确定是否应展现全新应用体验或恢复应用体验。