"Win 10 App development" Multi-window view

Source: Internet
Author: User

In general, Windows apps can have only one instance of an application running at a time, providing multi-view operation support for simultaneous rendering of different ui,sdk under special requirements.

An application can create new app views that, based on new views, can render content that differs from the main view without affecting the UI of the main viewing. These views can be toggled either in the same window or in a new window to render a new view. These windows, users can drag and drop to different virtual desktops.

In fact, the creation of views, switching, display is not difficult, the main difficulty is that the types required to complete these operations are distributed in different namespaces, so not familiar with the SDK's friends may not be found.

The API for view management is mainly distributed under the following two namespaces:

Windows.ApplicationModel.Core
Windows.UI.ViewManagement

There are two main classes used in the core. The Coreapplication class is responsible for creating the view, and calling the Createnewview method can create a new view that is returned after it is created with the Coreapplicationview object. Views created in the Coreapplication.views list, all created views are in this list while the application is running, so save resources and do not create views randomly.

In addition, in the Windows.UI.ViewManagement namespace, there are several classes, also used to manipulate the view, it is important that the above core is used to create the view, and viewmanagement under the class is used to manipulate a specific view.

The Applicationview class is used to get the view ID, set the view caption, and so on, where you can also customize the window title bar, the background color of the title bar button, and the foreground color by relying on the Applicationviewtitlebar class.

To display a view on a new window, or to switch views, is done by the Applicationviewswitcher class, which is static and can be played directly without instantiation.

Here's an example of how to put a few web links on the main view and click on a link to open the Browse target page in a new window.

The core code is as follows:

            //Create a new viewCoreapplicationview Newview =NULL; if(CoreApplication.Views.Count >1) {Newview= coreapplication.views[1]; }            //without this view, create a            if(Newview = =NULL) {Newview=Coreapplication.createnewview (); }            intNewviewid =default(int); //Initializing views//Note that the corresponding thread must be executed on the            awaitNewView.Dispatcher.RunAsync (Windows.UI.Core.CoreDispatcherPriority.Normal, ()=                {                    //There are two ways to get the view ID//method One: Getapplicationviewidforwindow method, note the thread to correspond//window must be the one associated with the current view//int viewID = Applicationview.getapplicationviewidforwindow (Newview.corewindow); //method Two: The simplest//because the code that is currently executing is on the UI thread of the new view .//So what Getforcurrentview is returning is the new view you just created.Applicationview TheView =Applicationview.getforcurrentview (); //set the title of the new window (optional)Theview.title =content; //You must note the view IDNewviewid =theview.id; //initializing the UI of the ViewUcdisplaypage UC = Window.Current.Content asUcdisplaypage; if(UC = =NULL) {UC=NewUcdisplaypage (); Uc. HorizontalAlignment=Horizontalalignment.stretch; Uc. VerticalAlignment=Verticalalignment.stretch; Uc. MinWidth=450d; Uc. MinHeight=300d; Window.Current.Content=UC; } UC. Targetwebpageuri=URI;                    Window.Current.Activate (); //The Activate method must be called, otherwise the view cannot be displayed                    /*Note: In the App class, Window.current gets the main view (at least one of the views when the program starts, or the user can't see the hairs).                    Because the code here is executed on the UI thread of the newly created view, window.current naturally gets the window in which the new view is located. */                }); //Start Show New View            BOOLb =awaitApplicationviewswitcher.tryshowasstandaloneasync (NEWVIEWID); if(b) {//The new view is displayed successfully            }            Else            {                //View display failed}

There is really no difficulty, the key point is that you have to understand. Coreapplication.createnewview creating a view this should not be difficult to understand, but when initializing the UI of a new view, it must be 700% note that each view is managed by a separate UI thread. So, your code is written in the main view, you can't access the new views directly in the main view, you have to do it from the dispatcher associated with the new Views (Coreapplicationview objects).

In the code that is inserted into the dispatcher queue, Window.current refers to the window that is not the main view, in the app class to access window.current of course, return is the primary window, but because the view is distributed on different UI threads, the code executed in the dispatcher of the new views, window.current get a new window A reference to the port.

At this time, you can set the UI object to the content property of the window and arrange the contents of the new window to be displayed as usual. In my example is a user control, this does not need me to introduce, all have done win development, whether you are WPF or WinForms, certainly know that the user control, is to use the existing control two times assembly, than to re-develop a control convenient.

Because you use Applicationviewswitcher to display or switch views using the View ID (an integer, which is often a negative value, such as-3122), we must obtain the ID number of the new view. To use the Applicationviewswitcher class to display it.

One approach is to access the Applicationview.getapplicationviewidforwindow method in the code that dispatcher inserts, which can get the view ID from the window to which the new view belongs.

Another easiest way is to get a reference to the current view directly with the Applicationview.getforcurrentview method, because this line of code is written on the UI thread of the new view, so it gets to the nature of the new view's reference. The Getforcurrentview method is called on which thread, and it gets the view associated with that thread .

The last key is to make sure that the window's Activate method is called after the window is arranged on the thread of the new view, that the window is activated, or that the window stays on the splash screen forever.

In Win 8.1, you do not call the Activate method also does not matter, because the 8x application is full screen, and 10x application is both full-screen, can also be windowed, so you must call the Activate method. The principle and practice is the same as the Onlaunch method of initializing the app.

OK, the key points for everyone to analyze, the focus is that we can not understand, programming this thing is so, understand the easy, do not understand the brain pain.

To run, click on the link on the main window to open the page in a new window. And you can drag a new window onto another virtual desktop.

Sample source code Download: Http://files.cnblogs.com/files/tcjiaan/MultiViewApp.zip

"Win 10 App development" Multi-window view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.