Windows Ten (UWP) Development Tips-Pageusercontrol

Source: Internet
Author: User

"This series needs to have a certain development foundation"

We often encounter this scenario in development:

1. Render the details and include some actions. such as: View the original, support zoom, zoom out, multi-image.

2. Perform a specific behavior and have a receipt result. such as: Select a contact, select a graph, user login.

A common solution is to encapsulate a UserControl onto a page and control its implicit. If the function is very small, that doesn't matter, can be slightly more complicated, packaged into a separate page is not better? You can also save resources on the current page. This method can solve scenario 1, but scenario 2 requires a receipt result, and what to do, always can not use the global variable bar. Pageusercontrol is the primary solution to these problems, encapsulation, which encapsulates a page containing a particular logic into a pseudo-control, so that it can be called separately, and can feedback execution results.

Calling methods

Pageusercontrol

Pageusercontrol is an abstract generic class that acts as the parent class for the encapsulated control. Principle: Listen to the navigated event of the frame, use the cached two page variables, distinguish whether it is forward or back, and then do the value and value operation respectively. Nonsense not much to say, directly on the code:

     Public Abstract classPageusercontrol<tpage>whereTpage:page {Private Const string_framenameinframepage ="Childrenframe"; PrivateFrame _frame; Private Object_framecontentwhenopened; PrivateTpage _page; /// <summary>        ///gets whether the precedence is rendered in childrenframe. /// </summary>         Public BOOLIschildrenframefirst {Get;protected Set; } #regionMethodsprotected voidshowpage () { This.        Openpickerpage (); }        protected voidShowPage (Objectparameter) {             This.        Openpickerpage (parameter); }        //You need to implement this method if you need to return a value to the caller.         protected Virtual voidcommitvalue (Tpage page) {}Private voidOpenpickerpage (Objectparameter =NULL)        {            if(NULL==_frame) {_frame= Window.Current.Content asFrame; if(NULL!=_frame) {                    //here is the Convention mainpage page in Childrenframe is a child frame. //This method is not absolute, and there are still many flexible methods that can be extended, such as attaching properties to specify who is Childrenframe.                     if( This. Ischildrenframefirst && This. _frame. Currentsourcepagetype.equals (typeof(Pages.mainpage))) {varFramepage =(pages.mainpage) _frame.                        Content; varFrameinframepage = Framepage.findname (_framenameinframepage) asFrame; if(Frameinframepage! =NULL)                        {                             This. _frame =Frameinframepage; }} _framecontentwhenopened=_frame.                    Content; _frame. Navigated+=onframenavigated; _frame. navigationstopped+=onframenavigationstopped; _frame. NavigationFailed+=onframenavigationfailed; if(Parameter = =NULL) {_frame. Navigate (typeof(Tpage)); }                    Else{_frame. Navigate (typeof(tpage), parameter); }                }            }        }        Private voidClosepickerpage () {//Logoff Events            if(NULL!=_frame) {_frame. Navigated-=onframenavigated; _frame. navigationstopped-=onframenavigationstopped; _frame. NavigationFailed-=onframenavigationfailed; _frame=NULL; _framecontentwhenopened=NULL; }            //If the cached page has a value, try commit processing.             if(NULL!= This. _page) {                 This. Commitvalue ( This. _page);  This. _page =NULL; }        }        #endregion        #regionEventsPrivate voidOnframenavigated (Objectsender, NavigationEventArgs e) {            //if the back is done close processing, if forward the new page cache.             if(E.content = =_framecontentwhenopened)            {closepickerpage (); }            Else if(NULL== This. _page) {                varpage = E.content asTpage; if(Page! =NULL)                {                     This. _page =page; }            }        }        Private voidOnframenavigationfailed (Objectsender, Navigationfailedeventargs e)        {closepickerpage (); }        Private voidOnframenavigationstopped (Objectsender, NavigationEventArgs e)        {closepickerpage (); }        #endregion    }

The above code simply expands the frame to support rendering in the child frame (mainly considering the UWP's Spiltview), but the fixed constraints are not flexible. You crossing can expand on your own, for example, by using an attached property to identify a frame, which is not implemented here.

The use of the Pageusercontrol generic class is as follows:
 Public classImagechooser:pageusercontrol<imagechooserpage>    {         PublicImagechooser () {//priority is presented in Childrenframe.             Base. Ischildrenframefirst =true; }         Public voidShow () {Base.        ShowPage (); }        protected Override voidcommitvalue (Imagechooserpage page) {Base.            Commitvalue (page); //if the value of the page property that identifies the result is valid, the event is thrown to the caller.             if(!string. Isnullorwhitespace (page. Value)) { This. OnCompleted (page.            Value); }        }         Public EventEventhandler<chooseimagecompletedeventargs>completed; Private voidOnCompleted (stringimage) {            varHandler = This.            Completed; if(Handler! =NULL) {Handler ( This,NewChooseimagecompletedeventargs (image)); }        }    }     Public classChooseimagecompletedeventargs:eventargs { Public stringImage {Get;Private Set; } InternalChooseimagecompletedeventargs (stringimage) {             This. Image =image; }    }

The above code is for scenarios where the return value is required, or if you do not need to return a value, leave it blank or do not rewrite the Commitvalue method.

Note: Invoking page and control pages requires a navigationcachemode operation such as to ensure that the Pageusercontrol page variable is unique, for specific reasons refer to the Msdn-navigationcachemode property description.
         Publichomepage () { This.            InitializeComponent ();  This. Navigationcachemode =navigationcachemode.required; }        protected Override voidOnnavigatedfrom (NavigationEventArgs e) {Base.            Onnavigatedfrom (e); if(E.navigationmode = =navigationmode.back) { This. Navigationcachemode =navigationcachemode.disabled; }        }

How do I apply it correctly in MVVM mode? Using behavior!

Refer to Sample code ListPicker. In this sample code, a pageusercontrol named ListPicker is encapsulated, which accepts the Itemssources,itemtemplate,selecteditem parameter, Corresponds to the same property of the ListView in Listpickerpage. The showlistpickeraction encapsulates the call to ListPicker.
        <ButtonContent= "Picture"Grid.Row= "1">            <i:interaction. Behaviors>                <Core:eventtriggerbehaviorEventName= "click">                    <behaviors:showlistpickeractionItemsSource="{Binding Images}"ItemTemplate="{ThemeResource Imageitemtemplate}"Itemspickedcommand="{Binding Imagepickedcommand}"Itemspickedinputconverter="{StaticResource Listpickeritemspickedeventargsconverter}"/>                </Core:eventtriggerbehavior>            </i:interaction. Behaviors>        </Button>

For a detailed implementation process, please refer to the example: click Open Link Https://github.com/rolerzhang/UWP-DevSkills

Reprint please indicate the source.

Windows Ten (UWP) Development Tips-Pageusercontrol

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.