我正在构建一个UWP应用程序,除了主视图之外还会生成第二个视图。
我想从主视图中关闭第二个视图。我已经尝试谷歌它,但大多数只告诉我如何产卵但不关闭。
下面是我用来打开一个新视图的代码块,在它的底部是我如何尝试关闭总是最终失败的新创建的视图。
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'
有关如何操作的任何链接或教程?
Nvm,伙计们我得到了答案。以下是我尝试从上面的代码关闭第二个视图的部分的更改:
await Task.Delay(5000).ContinueWith(async _ =>
{
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
CoreWindow.GetForCurrentThread().Close();
});
});