最小化或将其带到UWP应用程序后台时如何挂钩?

问题描述 投票:0回答:1

在我的 UWP 应用程序中,我显示一个弹出窗口,然后当用户 (1) 最小化应用程序(2) 将其置于后台时,我需要关闭它。

(1)为了最小化情况,我尝试了下面的代码。但结果是当应用程序窗口从最小化状态恢复时,弹出窗口消失。这不好。

我期望的是当应用程序窗口最小化时,弹出窗口立即关闭。

(2)至于采取背景的情况,我不知道。请指教。

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;
}
c# windows uwp
1个回答
0
投票

根据您的场景,建议您在页面代码隐藏中添加

EnteredBackground
事件,以方便调用弹窗。

Application.Current.EnteredBackground += Current_EnteredBackground;  

主页.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;
}
© www.soinside.com 2019 - 2024. All rights reserved.