UWP understand the concept of view, so UWP application to display multiple windows (multi-view)

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?


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):

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:

CoreApplicationAll management view (View) a UWP applications, while CoreApplicationdirect management view is CoreApplicationView; that is, UWP application CoreApplicationto manage all the applications view CoreApplicationView. While one CoreApplicationViewcontains a window and a thread scheduling model, that is, CoreWindowand CoreDispatcher.

CoreWindowWe understand that window. For ease of use, Windows.UI.XAML.Windowthe type of the package CoreWindow. CoreDispatcherThread 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 CoreApplicationand CoreWindowthe relationship between, understanding the role of the news cycle in the application.

Here Insert Picture Description

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. CoreApplicationStatic method CreateNewView, after the call to create new CoreApplicationView, which contains a complete CoreWindowand CoreDispatcher.

Here Insert Picture Description

Creates and displays a new CoreApplicationViewcode 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 multi-window

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.

CoreApplicationThere is a MainViewproperty 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.

CoreApplicationThere are Viewsattributes of all stored CoreApplicationView, we can use this collection to manage multiple views. Use ApplicationViewSwitcher.SwitchAsyncand 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.

Creative Commons License

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 .

Published 432 original articles · won praise 259 · views 560 000 +

Guess you like

Origin blog.csdn.net/WPwalter/article/details/104573379
UWP