在ApplicationViewSwitcher.SwitchAsync之后阻止Application.EnteredBackground

时间:2017-08-19 15:56:52

标签: uwp

我正在应用中实施密码保护。为此,我通过ApplicationViewSwitcher.SwitchAsync()显示其他视图。要在将应用程序发送到后台后请求密码我正在使用Window.Current.Activated事件,我在EnteredBackground中使用了该事件。 Hovewer这引发了Window.Current.Activated / Application.EnteredBackground的无限循环

public App()
{
    this.InitializeComponent();
    this.EnteredBackground += OnEnteredBackground;
}

private int _mainId;

private void OnEnteredBackground(object sender, EnteredBackgroundEventArgs e)
{
    if (_mainId == 0 && CoreApplication.GetCurrentView().IsMain)
    {
        Window.Current.Activated -= Current_Activated;
        Window.Current.Activated += Current_Activated;
    }
}

private async void Current_Activated(object sender, WindowActivatedEventArgs e)
{
    if (e.WindowActivationState != CoreWindowActivationState.Deactivated)
    {
        Window.Current.Activated -= Current_Activated;
        if (CoreApplication.GetCurrentView().IsMain)
        {
            await ShowPasswordPromptAsync();
        }
    }
}

private async Task ShowPasswordPromptAsync()
{
    _mainId = ApplicationView.GetForCurrentView().Id;

    var passwordView = CoreApplication.CreateNewView();
    int newViewId = 0;
    await passwordView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
        () =>
        {
            newViewId = ApplicationView.GetForCurrentView().Id;

            Frame frame = new Frame();
            Window.Current.Content = frame;
            frame.Navigate(typeof(View.PINProtectionPage));
            // You have to activate the window in order to show it later.
            Window.Current.Activate();
        });
    await ApplicationViewSwitcher.SwitchAsync(newViewId, _mainId, ApplicationViewSwitchingOptions.SkipAnimation);
}

public async Task SwitchBackToMainViewAsync()
{
    var currentViewId = ApplicationView.GetForCurrentView().Id;
    if (_mainId != 0 && currentViewId != _mainId)
    {
        await ApplicationViewSwitcher.SwitchAsync(_mainId, currentViewId, ApplicationViewSwitchingOptions.SkipAnimation);
        _mainId = 0;
    }
}

有什么想法吗?

0 个答案:

没有答案