Gilberto
Gilberto

Reputation: 77

Invoke user control/main page method when UWP app gets suspended

I have a custom user control (UWP) I need to invoke one of its methods when the UWP App enters on suspension mode OnSuspending method (App.cs), another approach is call a method located in MainPage

It is possible to achieve this ?

Upvotes: 1

Views: 284

Answers (1)

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

Your control can implement it's own OnSuspending handler, like this:

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
}

Upvotes: 2

Related Questions