[英]How to Save data on application close or entered background in WinUI3 app
How to intercept app_closing
or app_entering_background
in WinUi 3 app.如何在 WinUi 3 应用程序中拦截
app_closing
或app_entering_background
。
In UWP apps we do have Application_EnteredBackground
event, in which we can intercept app close, we use GetDeferral()
to save data.在 UWP 应用程序中,我们确实有
Application_EnteredBackground
事件,我们可以在其中拦截应用程序关闭,我们使用GetDeferral()
来保存数据。
Is there any same kind of event in WinUI 3 apps, I need to save data on app close, or entering background. WinUI 3 应用程序中是否有任何相同类型的事件,我需要在应用程序关闭或进入后台时保存数据。
Tried window_VisibilityChanged
and window_Closed
events, but not able to use GetDeferral()
.尝试了
window_VisibilityChanged
和window_Closed
事件,但无法使用GetDeferral()
。
Kindly help请帮助
Thanks in advance.提前致谢。
Noorul努鲁尔
Here is my test code for your reference, you can intercept the closing event.下面是我的测试代码供大家参考,可以拦截关闭事件。
(closing is executed before closed) (关闭在关闭之前执行)
public sealed partial class MainWindow : Window
{
private AppWindow _appWindow;
public MainWindow()
{
this.InitializeComponent();
this.Closed += OnClosed;
_appWindow = GetAppWindowForCurrentWindow();
_appWindow.Closing += OnClosing;
}
private void OnClosed(object sender, WindowEventArgs e)
{
string btnText = myButton.Content.ToString();
}
private void OnClosing(object sender, AppWindowClosingEventArgs e)
{
string btnText = myButton.Content.ToString();
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId myWndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(myWndId);
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.