在我的 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;
}
根据您的场景,建议您在页面代码隐藏中添加
EnteredBackground
事件,以方便调用弹窗。
Application.Current.EnteredBackground += Current_EnteredBackground;
<Grid>
<Popup x:Name="PopAccountInfo" IsOpen="True">
<Grid Width="100" Height="100" Background="Red"/>
</Popup>
</Grid>
public MainPage()
{
this.InitializeComponent();
Application.Current.EnteredBackground += Current_EnteredBackground;
}
private void Current_EnteredBackground(object sender, Windows.ApplicationModel.EnteredBackgroundEventArgs e)
{
this.PopAccountInfo.IsOpen = false;
}