如何关闭uwp中的二级视图

How to close secondary view in uwp

我正在构建一个 UWP 应用程序,它会在主视图之外生成第二个视图。

我想关闭主视图中的第二个视图。 我已经尝试 google 它,但大部分只告诉我如何生成而不是关闭。

下面是我用来打开新视图的代码块,底部是我如何尝试关闭新创建的视图,但总是以失败告终。

    public async void OpenNew(Type type, string title, string uniqueId)
    {
        CoreApplicationView newView = null;
        if (!IsExists(uniqueId))
        {
            newView = CoreApplication.CreateNewView();

            int newViewId = 0;
            await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                Frame frame = new Frame();
                Window.Current.Content = frame;
                Window.Current.Activate();
                frame.Navigate(type);

                newViewId = ApplicationView.GetForCurrentView().Id;
                ApplicationView.GetForCurrentView().Title = title;
                ApplicationView.GetForCurrentView().Consolidated += OnConsolidated;
            });

            await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
            _uniqueIds.Add(uniqueId, newViewId);
        }

        // Below code testing on how I try to close newly spawned view but always throw error
        await Task.Delay(5000).ContinueWith(async _ =>
        {
            await Window.Current.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                newView.CoreWindow.Close();
            });
        });
    }

这段代码总是抛出

Exception thrown: 'System.NullReferenceException'

任何link或教程如何做到这一点?

Nvm,伙计们,我得到了答案。以下是我尝试从上面的代码关闭第二个视图的部分的更改:

await Task.Delay(5000).ContinueWith(async _ =>
{
    await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        CoreWindow.GetForCurrentThread().Close();
    });
});