UWP is a multi-application window to complete all business, in fact, this way I also recommend the use of a single window. However, there are always some special cases we need to use more than one window, then the UWP how to use multiple windows do?
In this article
Why UWP need multi-window?
Multi-window development among traditional Win32 is commonplace thing, but I personally do not like it, because multiple windows on Windows systems too pit. The following are some of the pits on traditional multi-window Development I have previously written (in addition to more):
- After closing the modal window, the parent window actually went behind other windows
- WPF program can not touch the operation? Together we came to the causes and solutions!
The reason is very simple to use multi-window - allows users to multi-tasking. From this perspective, the traditional Win32 using the "modal" multi-window mode is simply inefficient but also to bring Bug!
Microsoft's official documentation lists some examples: for example, while writing e-mail messages while the reference in the past; while watching music being played while browsing the playlist; open more than one time and then read the article together later and so on.
The concept UWP view
Before learning how to write UWP multi-window, we need to understand some of the concepts UWP view (View) is.
The difference between CoreApplication / Application, CoreWindow / Window in an article, I described some of the concepts UWP view:
CoreApplication
All management view (View) a UWP applications, whileCoreApplication
direct management view isCoreApplicationView
; that is, UWP applicationCoreApplication
to manage all the applications viewCoreApplicationView
. While oneCoreApplicationView
contains a window and a thread scheduling model, that is,CoreWindow
andCoreDispatcher
.
CoreWindow
We understand that window. For ease of use,Windows.UI.XAML.Window
the type of the packageCoreWindow
.CoreDispatcher
Thread scheduling model is based on the message loop, it is because of the news cycle, so this window can always displayed without being destroyed.
In order to start the process of understanding UWP, UWP I created a program from scratch in one article, we can understand CoreApplication
and CoreWindow
the relationship between, understanding the role of the news cycle in the application.
UWP multi-window
After UWP understand the concept of view, strictly speaking, the title of this section should be called "UWP multi-view."
I drew a mind map to describe the relationship between them. CoreApplication
Static method CreateNewView
, after the call to create new CoreApplicationView
, which contains a complete CoreWindow
and CoreDispatcher
.
Creates and displays a new CoreApplicationView
code as follows:
private async void OnLoaded(object sender, RoutedEventArgs e)
{
// 创建一个 CoreApplicationView,即新的应用视图。
var applicationView = CoreApplication.CreateNewView();
// 一个应用视图有自己的 Id,稍后我们创建应用视图的时候,需要记录这个 Id。
int newViewId = 0;
// 使用新应用视图的 CoreDispatcher 线程调度模型来执行新视图中的操作。
await applicationView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// 在新的应用视图中,我们将新的窗口内容设置为 ThePageInNewView 页面。
Frame frame = new Frame();
frame.Navigate(typeof(ThePageInNewView), null);
Window.Current.Content = frame;
Window.Current.Activate();
// 记录新应用视图的 Id,这样才能稍后切换。
newViewId = ApplicationView.GetForCurrentView().Id;
});
// 使用刚刚记录的新应用视图 Id 显示新的应用视图。
var viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}
After the creation of the following picture:
UWP manage multiple views
We usually develop when the UWP applications rarely care about CoreApplicationView
, because by default UWP can do a lot of work management application view for us.
CoreApplication
There is a MainView
property that we started to run the application view when the UWP applications. If we have more than one application view is displayed, then the main window then click the close button will no longer be closed, but hidden. If you want to turn off, you need to call Application.Exit
.
CoreApplication
There are Views
attributes of all stored CoreApplicationView
, we can use this collection to manage multiple views. Use ApplicationViewSwitcher.SwitchAsync
and pass the view may switch display views Id.
await ApplicationViewSwitcher.SwitchAsync(viewIdToShow);
Reference material
My blog will be starting in https://blog.walterlv.com/ , and will be released from CSDN which featured, but it is rarely updated once released.
If you see any do not understand the contents of the blog, please share. I built dotnet Vocational and Technical College welcome to join.
This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Lu Yi (containing links: https://walterlv.blog.csdn.net/ ), shall not be used for commercial purposes, a work based on the article be sure to modify the same license release. If you have any questions, please contact me .