X:bind of "WIN10" uap/uwp/general development

Source: Internet
Author: User

[Some information relates to pre-released product which could be substantially modified before it ' s commercially released. M Icrosoft makes no warranties, express or implied, with respect to the information provided here.]

[The version involved in the pre-release of a message may be substantially modified in its commercial version.] Microsoft makes no warranties regarding the information provided herein. ]

In MSDN, the Windows SDK on the east, will declare this sentence, I also lead the bar, he does not guarantee, I can not protect.

Body

In Win10 UWP development, a new keyword X:bind was added. Where is it? Why would you use him?

Build Conference Video resources: Http://www.microsoftvirtualacademy.com/training-courses/a-developers-guide-to-windows-10-preview? Prid=ch9courselink

One, X:bind good?

From these two paintings can be seen, x:bind performance is better than binding. Why?

This binding is known as the "compiled data bindings", which literally looks at the compiled databinding.

The binding we used before is the runtime (run time) bind, which is determined at compile time.

The advantage is high efficiency, fast, binding errors at compile time will prompt out, convenient debugging.

Second, X:bind

Main points of X:bind:

1. Strong type

2. Context is page or UserControl

3. The default mode for bindings is onetime

When we use binding, sometimes we don't have to think about types, because there are a lot of default transformations (such as TypeConverter), but when you use X:bind, you get an error if the type does not match the compilation.

For example:

<IsChecked= "{x:bind}"/>

We bind the current context to the IsChecked (BOOL)

Invalid binding path ': Cannot bind type ' binddemo.mainpage ' to ' System.Nullable (System.Boolean) ' Without a converter

That's what it turns out to be. Of course, if you are bound to a string type of property is not wrong, he will help you ToString () to the ground.

The binding context, which now appears to be the current class itself, is inherited from Page and UserControl itself.

For beginners in the classmate, no longer afraid to find the object, also do not set DataContext.

To be familiar with MVVM classmate, want to use X:bind to write ViewModel to Codebehand in one, really strange say, a lot of frame may also have to adjust.

Three, small test sledgehammer

    <GridBackground="{ThemeResource Applicationpagebackgroundthemebrush}">        <TextBlockText="{X:bind}"VerticalAlignment= "Center"HorizontalAlignment= "Center" />    </Grid>

Codebehand does not make any code.

Results:

From this result can also be seen, his context, is the current page.

Let's add a title attribute to the Codebehand.

     Public Sealed Partial class Mainpage:page    {        publicstringgetset'Bind Demo' ;          Public MainPage ()        {            this. InitializeComponent ();        }    }
    <GridBackground="{ThemeResource Applicationpagebackgroundthemebrush}">        <TextBlockText="{x:bind Title}"VerticalAlignment= "Center"HorizontalAlignment= "Center" />    </Grid>

Here, the person familiar with MVVM is going to think, how do we bind ViewModel? Actually is the ViewModel object, writes in the Codebehand one is good.

     Public class Mainviewmodel    {        publicstringgetset'Bind';         Publicstringgetset"Demo" ;    }
     Public Sealed Partial classMainpage:page { Public stringTitle {Get;Set; } ="Bind Demo";  PublicMainviewmodel ViewModel {Get;Set; } =NewMainviewmodel ();  PublicMainPage () { This.        InitializeComponent (); }    }
    <GridBackground="{ThemeResource Applicationpagebackgroundthemebrush}">        <StackPanelHorizontalAlignment= "Center">            <TextBlockText="{x:bind Title}" />            <TextBlockText="{x:bind Viewmodel.name}" />            <TextBlockText="{x:bind Viewmodel.content}" />        </StackPanel>    </Grid>

Results

Iv. use in Data templates

Because X:bind is strongly typed, so when you use DataTemplate, to specify X:datatype, we rewrite the example above.

The Codebehand code does not change, we change directly into XAML. Add a key to the Testdatatmpleate data template, in order to distinguish, we give a background color, the order is also changed.

<Pagex:class= "Binddemo.mainpage"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local= "Using:binddemo"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"mc:ignorable= "D">    <page.resources>        <DataTemplatex:key= "Testdatatemplate"X:datatype= "Local:mainviewmodel">            <StackPanelBackground= "Orange">                <TextBlockText="{x:bind Content}" />                <TextBlockText="{x:bind Name}" />            </StackPanel>        </DataTemplate>    </page.resources>    <GridBackground="{ThemeResource Applicationpagebackgroundthemebrush}">        <StackPanelHorizontalAlignment= "Center">            <TextBlockText="{x:bind Title}" />            <ContentControlContentTemplate="{StaticResource Testdatatemplate}"Content="{x:bind ViewModel}" />        </StackPanel>    </Grid></Page>

Results:

If we don't set the X:datatype. He's going to tell us the error.

V. Binding an event to a method

X:bind is a support event bound to a method, for example I add a test method to Mainviewmodel. Then add a button to the data template.

Click this don't think all support, I just try a pointerentered event.

This method of binding, can not write parameters, you can also write the parameters of the event full.

        <DataTemplatex:key= "Testdatatemplate"X:datatype= "Local:mainviewmodel">            <StackPanelBackground= "Orange">                <TextBlockText="{x:bind Content}" />                <TextBlockText="{x:bind Name}" />                <Buttonpointerentered="{x:bind Test}" />            </StackPanel>        </DataTemplate>

Perfect entry breakpoint.

Vi. Summary

Now the binding can also be used together, at least without the table X:bind can complete the replacement of the binding, at least in the dynamic type of binding, X:bind is not playing. and using x:bind how much disrupts the structure of the MVVM we're accustomed to.

But X:bind brings the performance of the promotion, really let people heart, can, if possible to use this.

This article is only my x:bind preliminary understanding, also did not find any document, if has the wrong place, please everybody points out. Thank you.

This address: http://www.cnblogs.com/gaoshang212/p/4534138.html

X:bind of "WIN10" uap/uwp/general development

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.