"Windows 10 App development" uses x:bind tags to dynamically get results

Source: Internet
Author: User

The UWP introduces the bind tag on the traditional (WPF) binding tag, and bind is generated based on the compilation phase, thus having high performance. However, you should note that this performance optimization eliminates the overhead of dynamic binding at run time, which does not include the volume of the data source. If your data source itself is very large, there is no way to optimize, you can only rely on your own ways, as far as possible to load into the memory of the data to reduce, such as you only load 300, do not immediately read tens of thousands of data. Of course, you can also use an incremental load scheme.

The bind tag also has a feature-you can bind the event-handling method and the general method to pass parameters to the bound method. For example, there is a method:

int int int y)

You can bind like this:

<prop= "{x:bind Add (2,5)}"/>

You can also type-cast during use.

< obj    = "{x:bind (button) parm). Content}"   ... />

You have to be aware that because the bind token is completed during the compile phase, you must ensure that the conversion is valid when the type is cast. To be blunt, a type must be able to be turned, such as an error at compile time.

<  = "{x:bind (string) Add (2,8)}" ...  />

Because the int type is not directly cast to the string type.

Bind tag Although the force is very high, but the force is the limit, for the moment, bind tag and method binding function is still limited, after all, is not so easy to identify the syntax, in the future may become stronger.

For now, BIND has the following limitations:

A, cannot bind nested methods. this will cause the bindings to hang.

<= "{x:bind sys:Math.Min (sys:int. Parse (txt1. Text), Sys:int. Parse (txt2. Text)}"/>

When the Min method is called, the invocation of the Parse method is nested, and it is not possible to do so at compile time.

B, the type of the parameter and the return value must match . For example, the Add method that we have previously cited, which accepts two parameters of type int, is problematic if we write this.

<   = "{x:bind Add (Txtbox1.text, Txtbox2.text)}" ... />

The textbox's Text property is a string type, and a direct pass to an int type parameter is an error.

Again, for example, this writes back.

<   = "{x:bind Add (3,6)}" ... />

The Add method returns a value of type int, and the Text property of the TextBlock class is of type string, and an error is also found here.

When referencing other XAML elements, the Bind tag does not need to use the ElementName property as the Binding element to specify the name. Just give the corresponding XAML element a name and then you can refer directly to it. Like this.

<x:name= "Txtfirst"/><= " {x:bind Txtfirst.text} " />

To bind the Text property of the TextBlock object to the Text property of the preceding textbox, the textbox is the data source, so long as it has a name, then it can be referenced directly, Txtfirst.text.

Here is a simple example of how to use X:bind to install X.

The example is this: Two text boxes, enter values separately, and then dynamically display the larger of the two values below. For example, if you enter 7 and 11, then the 11 is displayed.

Here we have to consider the limitations, because the TextBox class's Text property is a string type, so it cannot be directly bound to the math class's Max method, which makes it difficult to type conversion, so we'd better do the encapsulation.

     Public classTest { Public Static stringMaxnum (stringS1,stringS2) {            DoubleD1, D2; if(Double. TryParse (S1, out Doubletmp)) D1=tmp; ElseD1=0d; if(Double. TryParse (S2, out Doubletmp2)) D2=TMP2; ElseD2=0d; Doubleres =Math.max (D1, D2); returnRes. ToString ("N"); }    }

The calculation method is defined as static on the line, so that can be played directly.

The parameter received by this method is a string type, the return value is also a string type, why, you see the following XAML to know.

Next, look at the interface layout.

        <StackPanelMargin= "+">            <TextBoxx:name= "TxtNum1"Header= "First value:"/>            <TextBoxx:name= "TxtNum2"Header= "Second value:"/>            <TextBlock>                <RunText= "One of the larger numbers is:"/>                <RunForeground= "Blue"FontSize= "+"Text="{x:bind local:Test.MaxNum (txtnum1.text,txtnum2.text), mode=oneway}"/>            </TextBlock>        </StackPanel>

Because a Run element is displayed for a larger value, its Text property is a string type, and in order to avoid the error of type conversion, the Maxnum method just returned the string type.

This place, in order to explicitly mode set OneWay, otherwise it will use OneTime to bind, so that can not dynamically get the results of the calculation.

Well, run it, assuming input 200.65 and 105.33, show the results as shown.

Then we change 200.65 to-300, as a result.

You will find that the values shown below are updated in real time as the input changes.

Is it a lot of force to play this way?

Please click here to download the complete sample code.

"Windows 10 App development" uses x:bind tags to dynamically get results

Related Article

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.