". Net deep breathing" real-time access to calculation results

Source: Internet
Author: User

The last old week introduced the use of X:bind tags in UWP apps to bind to methods to achieve real-time access to calculations. Today, let's play with the implementation of WPF above.

Although WPF does not have a x:bind tag (not for the time being), WPF has a very powerful engine d in data binding, after all, it is part of the. NET Framework. In fact, when in. NET 1.x, the old week speculated that Microsoft would introduce a new framework, but did not know it was called WPF, why. Because at that time old week found that the Windows forms application related types are placed under the System.Windows.Forms namespace, do not know you beginner. NET will feel very strange, anyway old weeks feel very strange, why not directly put to System.Windows namespace, so, I thought, the System.Windows namespace will definitely put something else in the future.

Later,. NET 2.0 Sp1,.net 3.0, especially. NET 3.5, the framework was gradually stereotyped, and I always thought 4.0 was a sign of. NET maturity.

Well, not in history class, let's go on with the programming lesson.

In WPF, a very interesting binding model is provided--datasourceprovider, of course, it is abstract class, can not be used directly, the words will be convulsions. Its derived class implements two forms of "indirect binding" for us: XmlDataProvider can be a component or an XML document outside of a link, ObjectDataProvider can be bound to a type or instance object of that type, and can also be bound to a method member of a type. Either an instance method or a static method.

To get the results in real time, you should use the ObjectDataProvider class, if you want to bind an instance method, except to set the type of the target type for the ObjectType property, assign an instance of that type to the ObjectInstance property If you are binding to a static method, you do not have to specify the ObjectInstance property.

Then, by setting the name of the method to bind by the MethodName property, if the method has parameters, add elements to the Methodparameters list, several values are passed, and the order of the elements is consistent with the declaration of the method parameter. Note that this method has a return value, because the binding target obtains the computed result from the method.

Let's use an example to illustrate this.

First, you have to declare a class, for the calculation, I will use a static method, simple and convenient.

     Public class Demo    {        publicstaticdouble getmin (doubledouble  b)        {             return Math.min (A, b);        }    }

This method is very simple, kindergarten level, enter two values, return the smaller values.

Then, the point is to declare the ObjectDataProvider instance in the XAML document.

        <grid.resources>            <Local:stranddoubleconverterx:key= "CVT"/>            <ObjectDataProviderx:key= "DPRD"ObjectType="{x:type Local:demo}"MethodName= "Getmin">                <objectdataprovider.methodparameters>                    <sys:double>0</sys:double>                    <sys:double>0</sys:double>                </objectdataprovider.methodparameters>            </ObjectDataProvider>        </grid.resources>

Stranddoubleconverter is a custom converter that transforms between a string and a double, which you can ignore.

The ObjectType property specifies the Demo class that we just defined, and the MethodName method specifies just that static method. Since this method requires two input parameters, you have to add two double values to the Methodparameters, which is set to 0 by default.

Here we put two TextBox controls on the UI to enter two values, and automatically pass the value to the Methodparameters list of the ObjectDataProvider object when the input value changes. How to achieve it? Data binding, which binds the TextBox's Text property to the elements in the methodparameters, and is a two-way binding.

            <TextBoxGrid.Row= "0"Grid.column= "1"Margin= "4"Text="{Binding Source={staticresource dprd},bindsdirectlytosource=true,mode=twoway,path=methodparameters[0], Converter={staticresource cvt},updatesourcetrigger=propertychanged}"/>            <TextBoxGrid.Row= "1"Grid.column= "1"Margin= "4"Text="{Binding Source={staticresource dprd},bindsdirectlytosource=true,mode=twoway,path=methodparameters[1], Converter={staticresource cvt},updatesourcetrigger=propertychanged}"/>

Here, the Bindsdirectlytosource property remembers to be set to true, what does this mean? It means that the binding is directly bound to the object specified by the Source property, or to the original data source. In this binding model, the original data source should be the demo class, and the intermediate data source is the ObjectDataProvider object, so if you do not set the Bindsdirectlytosource property to True, the binding is relative to the demo Class, if the Bindsdirectlytosource property is true, then the relative source of the binding points to the ObjectDataProvider object, so that the Path property can find the elements in the Methodparameters list.

Where [0] represents the first element in the list, which is passed to the first parameter of the binding method, and, similarly, [1] is a pointer to the second argument. To be able to update the method parameters when the Text property changes, you need to set Updatesourcetrigger=propertychanged,mode=twoway.

Next, we declare a TextBlock element, which is also bound to the ObjectDataProvider object previously declared in the resource, and gets the result of the calculation in real time.

        <TextBlockGrid.Row= "1"Margin= "5">            <RunText= "The smaller that number:"/>            <RunText="{Binding Source={staticresource Dprd},mode=oneway}"Foreground= "Darkgreen"FontSize= "+"/>        </TextBlock>

Only the results can be read here, so Mode is OneWay. If the calculation takes a long time, you can set the Isasynchronous property to True, which optimizes the responsiveness of the UI.

Well, the example is done, let's see how it works.

The old week then attempts to change the return value of the method to void, assigning the result in the form of out and ref, but not getting the correct result after binding.

For sample source code download please click here.

". Net deep breathing" real-time access to calculation results

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.