[英]C# UWP closing application multiple views
I have a UWP app that dynamically creates another view. 我有一个动态创建另一个视图的UWP应用程序。 But, the problem is when I close the first window, the second still stays on.
但是,问题是当我关闭第一个窗口时,第二个窗口仍然保持打开状态。
With this code I am creating new view: 使用此代码,我创建了新视图:
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(SecondPage), null);
Window.Current.Content = frame;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
How to close the application when user close the first window? 用户关闭第一个窗口时如何关闭应用程序?
Please refer to Show multiple views for an app . 请参阅为应用显示多个视图 。
If secondary views are open, the main view's window can be hidden – for example, by clicking the close (x) button in the window title bar - but its thread remains active .
如果打开辅助视图,则可以隐藏主视图的窗口 - 例如,通过单击窗口标题栏中的关闭(x)按钮 - 但其线程保持活动状态 。 Calling Close on the main view's Window causes an
InvalidOperationException
to occur.在主视图的窗口上调用Close会导致发生
InvalidOperationException
。 (UseApplication.Exit
to close your app.) If the main view's thread is terminated, the app closes.(使用
Application.Exit
关闭您的应用程序。)如果主视图的线程终止,则应用程序将关闭。
private void Btn_Click(object sender, RoutedEventArgs e)
{
Application.Current.Exit();
}
You could also choose if you want to close the initial window and remove it from the taskbar by specifying the value of
ApplicationViewSwitchingOptions
.So that there is a single view on your desktop.您还可以选择是否要关闭初始窗口并通过指定
ApplicationViewSwitchingOptions
的值将其从任务栏中删除。因此桌面上只有一个视图。 You just need to close it for closing the application.您只需关闭它即可关闭应用程序。
private int currentViewId = ApplicationView.GetForCurrentView().Id;
private async void ShowNewView()
{
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(SecondPage), null);
Window.Current.Content = frame;
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
await ApplicationViewSwitcher.SwitchAsync(newViewId, currentViewId, ApplicationViewSwitchingOptions.ConsolidateViews);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.