UWP应用暂停时调用用户控件/主页方法

时间:2018-12-19 23:24:41

标签: c# .net uwp

我有一个自定义用户控件(UWP),当UWP App以暂停模式进入 OnSuspending方法(App.cs)时,我需要调用其方法之一,另一种方法是调用位于在MainPage中

有可能实现这一目标吗?

1 个答案:

答案 0 :(得分:2)

您的控件可以实现自己的OnSuspending处理程序,如下所示:

public MyUserControl()
{
    this.InitializeComponent();
    this.Loaded += MyUserControl_Loaded;
    this.Unloaded += MyUserControl_Unloaded;                
}

private void MyUserControl_Loaded(object sender, RoutedEventArgs e)
{
    Application.Current.Suspending += MyUserControl_Suspending;
}

private void MyUserControl_Unloaded(object sender, RoutedEventArgs e)
{
    Application.Current.Suspending -= MyUserControl_Suspending;
}

private void MyUserControl_Suspending(object sender, SuspendingEventArgs e)
{
    // your control's OnSuspending code here
}