Hello Silverlight 4 (Silverlight tutorial for beginners) Chapter 3

Source: Internet
Author: User
Tags silverlight
Data Service in silverlight4 (1)

[Size = 18px] [/size]

In chapter 2, we learned the controls. Through the layout controls, we can also create a program interface, which is quite convenient? Some people asked me what Silverlight is for. Before learning, I was confused about what Silverlight is for. However, we can make a business website home page for a competition, I believe that using Silverlight to create pages must be much faster and more stable than using HTML, which is a benefit.
No matter how beautiful your program interface is, there are many outstanding results. Without data interaction, it can only be regarded as a display program, and there is not much commercial value, people often access the Internet to obtain information. A website that cannot provide information in a timely manner cannot be regarded as a qualified website.

One thermometer
The following is an example of a thermometer to show what data binding is. If you have a basic knowledge, you can skip this section. A thermometer must be familiar to everyone. A thermometer often uses a mercury (or alcohol) column to indicate the current temperature. As the temperature changes, the indicated temperature value also changes, which means that the mercury (or alcohol) column has been bound to the temperature value. At this time, you do not need to change the temperature, the thermometer automatically updates the new temperature, that is, the bound image changes as the bound object changes. This is the data binding. At the same time, this example of a thermometer is also a good example of one-way binding. The so-called one-way binding means that the mercury (or alcohol) column will change with the temperature, however, the temperature will not change with the change of the mercury (or alcohol) column. If you put the thermometer in hot water, the mercury (or alcohol) column will rapidly increase, but in reality, the temperature will not change, because it is bound.
In contrast, there is also two way binding. either side of the binding changes, and the other side also changes. This is bidirectional binding.

Return to Silverlight
Okay, we can come back. There are three data binding methods in Silverlight.

One-time binding
If your program has a function to read the version number of the operating system, consider one-time binding, because data such as the operating system version number will not change, one-time binding is called one-time binding.

One-way binding
One-way binding is an example like the above thermometer.

Bidirectional binding
This binding method is mainly used for operations such as "Update". I enter a new value in a textbox to replace the old value in the database.

Bind data
There are already a lot of text in the front, and I think you can't wait to see how the research works.

Bind data
Binding data in Silverlight is not actually binding data, but binding "attributes". For new things like Silverlight, it is strange that "attribute" binding is not supported, this technology has been widely used in various programming. The so-called "attribute" is what we call the model data model.
Next we will create a data model that represents system information, that is, a class. As mentioned above, the variability of system information is very small, so we use one-time binding. We name this class MachineInfo, which can store the current machine. Net version information and the current Silverlight version information.
Right-click the project name, as shown in

Select newitem, select class in the pop-up dialog box, enter the class name MachineInfo in the text box below, and click Add.

Then we start to add the code.
Namespace Chapter3
{
Public class MachineInfo
{
Public MachineInfo ()
{
// Obtain information about the current operating system
Platform = system. environment. osversion. Platform. tostring ();
// The following if statement gets the Silverlight version information, so you do not need to pay attention to the details.
If (chapter3.app. Current. Host. isversionsupported ("4.0.41108.0 "))
{
Slversion = "4.0.41108.0 ";
}
Else
{
Slversion = "not latest ";
}
}
Public String platform {Get; set ;}
Public String slversion {Get; set ;}
Public void setup ()
{
}
}
}
Wow, a lot! But don't worry. In fact, the most important thing is only the blue code. Other code only assigns values to these two attributes, so that our model class has been written, of course, you can add more attributes by yourself. Although it is a little complicated to define, once defined, you will get a reusable model irrelevant to data access.

One-time binding practice
We want to get the following results:

Of course, the data is displayed differently on different machines, which is the benefit of data binding. Through the previous study, I think you should know how to create an interface similar to the above? I believe you.
We name the first textbox tb_platform, and the second textbox tb_slversion.
Because the control is described by XAML, we need to modify the XAML to get hands and feet on the Textbox Control (BIND data) (this is a little troublesome, I admit, but Sl is developing, maybe the next version will be improved)
Add a namespace of the model we created in the namespace Of The XAML Header

Xmlns: Local = "CLR-namespace: Chapter3"

Then add our model class name so that we can use the model class in XAML.

<Usercontrol. Resources>
<Local: MachineInfo X: Key = "MachineInfo"/>
</Usercontrol. Resources>

Don't be happy. It's not over yet.
Select tb_platform in the design view, and right-click the text attribute label on the right-side attribute panel.

Select applydatabinding.

Don't be attracted by the following Red Cross. Select staticresource

Select MachineInfo (not selected), so that our resources are selected. If there are no changes in the XAML, these options may not appear, currently, only one MachineInfo class is defined.
In the last step, find the following path tag, click "show", and select the attribute corresponding to the current Textbox Control.

These two attributes are defined previously. We can see that the data has been mapped out in XAML, thanks to the previous lines of XAML code.
Finally (--), select the following options tag to display, select onetime one-time binding in mode, and this will be OK. Please note that I did not write any background code.

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.