Dowemo

The bind tag is introduced on the traditional ( wpf ) binding tag, and bind is generated based on the compilation phase, which has a higher performance. However, you've to note that this performance optimization is free of overhead on the runtime dynamic binding, which isn't the volume of the data source. If your data source is very large, it isn't a way to optimize it, and you want to be able to make the load into memory as much as possible, such as you load only 300. You can also use incremental loading scenarios.

The bind tag also has a feature that allows you to bind an event handler and a general method to pass arguments to a bound method. For example, there's a way:





int Add ( int x, int y )





You can bind this:





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





In the use process, you can also type a type cast.





<obj prop ="{x:Bind ((Button)parm).Content}".. ./>





You must note that because the bind tag is completed during the compilation phase, you must ensure that the conversion is valid when the type is cast. The core is that the type must be able to be able to turn, such as this when compiling.





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





Because an int type isn't cast directly to a string type.

The bind tag is very high, but it's bound to have a limit, and the function of bind tag and method binding is limited, after all, it isn't so easy to recognize.

For now, bind has the following limitations:

A, cannot bind nested methods. This binding is suspended.





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





When the min method is called, the call to the parse method is nested, which isn't available at the time of compilation.

B, the type of the parameter and return value must match For example, the add method for our previous example, it takes two parameters of the int type, and if we write it, it's a problem.





<obj prop ="{x:Bind Add(txtBox1.Text, txtBox2.Text)}".. ./>





The text property of the textbox is a string type that's passed directly to the int type argument.

For example, write back.





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





The add method retur & the value of an int type, and the text property of the textblock class is the string type, which is also an error.

When referencing other xaml elements, the bind tag doesn't need to specify a name by using the 3gpp property like the binding element. As long as you name the corresponding xaml element, you can reference it directly. Like this.





<TextBox x:Name="txtFirst" />


<TextBlock Text ="{x:Bind txtFirst.Text}" />





To bind the text property of the textblock object to the text property of the previous textbox, the textbox is a data source, as long as it's a name, then the txtFirst.Text can be referenced directly.

Let's show you a simple example, one with x:Bind to mount the.

This example is that: Two text boxes, input values, and then dynamically display a larger one in the two values. For example, you've entered 7 and 11, then display 11.

Here we've to consider limitations because the text property of the textbox class is a string type, so it isn't possible to bind directly to the math class 's max method, so we're better.





 public class Test


 {


 public static string MaxNum(string s1, string s2)


 {


 double d1, d2;


 if (double.TryParse(s1, out double tmp))


 d1 = tmp;


 else


 d1 = 0d;



 if (double.TryParse(s2, out double tmp2))


 d2 = tmp2;


 else


 d2 = 0d;


 double res = Math.Max(d1, d2);


 return res.ToString("N");


 }


 }





Define the calculation method as static, so you can get it directly.

The parameter received is a string type, and the return value is also a string type, and why you see the following xaml.

Next, look at the interface layout.





 <StackPanel Margin="16">


 <TextBox x:Name="txtNum1" Header="第一个数值:"/>


 <TextBox x:Name="txtNum2" Header="第二个数值:"/>



 <TextBlock>


 <Run Text="其中较大的一个数是:"/>


 <Run Foreground="Blue" FontSize="16" Text="{x:Bind local:Test.MaxNum(txtNum1.Text,txtNum2.Text),Mode=OneWay}"/>


 </TextBlock>


 </StackPanel>





Because the larger numeric value is the run element, its text property is a string type, so the maxnum method is returned to return a string type in order to avoid.

In this place, you explicitly set mode to set oneway, or it'll be bound with onetime so that it cannot be dynamically obtained.

All right, let's run it, assuming that you enter 200.65 and 105.33, the results shown are shown below.

And then we change 200.65 to -300, the results are as follows.

It's found that the values shown below are updated with the input changes.

If you want to do this, it's a.

Complete sample code please click here.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs