How to hook when minimize or bring it to background of an UWP app?

41 Views Asked by At

In my UWP application, I show an popup and then I need to close it when users (1) minimize the application or (2) bring it to background.

(1) For minimize case, I tried below codes. But the result is when application window is restored from minimized state, the the popup disappears. This is not good.

What I expect is when application window is minimized, the popup is closed at once.

(2) As for the case of taken to background, I have no idea. Please advise.

App.xaml.cs:

this.EnteredBackground += App_EnteredBackground;

 private void App_EnteredBackground(object sender, EnteredBackgroundEventArgs e)
 {
     AppEnteredBackground?.Invoke();
 }

MainPage.xaml.cs:

App.AppEnteredBackground += App_EnteredBackground;
private void App_EnteredBackground()
{
    this.PopAccountInfo.IsOpen = false;
}
1

There are 1 best solutions below

0
Junjie Zhu - MSFT On

Depending on your scenario, it is recommended that you add an EnteredBackground event to the codebehind of the page to make it easier to call the popup.

Application.Current.EnteredBackground += Current_EnteredBackground;

MainPage.xaml

<Grid>
    <Popup x:Name="PopAccountInfo" IsOpen="True">
        <Grid Width="100" Height="100" Background="Red"/>
    </Popup>   
</Grid>

MainPage.xaml.cs

public MainPage()
{
    this.InitializeComponent();
    Application.Current.EnteredBackground += Current_EnteredBackground;    
}

private void Current_EnteredBackground(object sender, Windows.ApplicationModel.EnteredBackgroundEventArgs e)
{
    this.PopAccountInfo.IsOpen = false;
}