Continue the WPF series. This section will explain (XAML ,)
The XAML language, just getting started with WPF, is familiar with the XAML language. In fact, it is still a bit unfamiliar. The XAML evolved from XML. The Compiling Method Of The XAML language is more like the development of the Asp.net webpage, WPF divides a form into XAML, designer, and CS.CodeTo describe the style of the form with XAML. The designer can drag the control directly and write the CS code in the background.
The XAML syntax is described in the following ways,
1. object element syntax,
2. Special Line syntax <button click = "button?click" content = "WPF, Hello, world !" /> You can describe a widget in this way.
3. Attribute element syntax
<Button>
<Button. Background> Red </button. Background>
</Button>
4. Content syntax
<Button> Hello </button>
5. syntax for merging attribute elements and content
<Button> hello
<Button. Background> Red </button. Background>
</Button>
6. Mark extension syntax
TEXT = "{binding. elementname = xxx, Path = value }"
Generally, a WPF project is created. The XAML file consists of two parts: a window object and a grid object,
Window objects usually declare namespaces and other definitions. Grid is an empty layout without elements.
There are also a lot of la S, Data Banding, and other technologies in WPF, which will be mentioned later.
Correspondingly, since we can use the XAML syntax to generate controls, we can use C # code, just like writing CSProgramIn this way, you often need to dynamically add some controls.
Button BTN = new button ();
BTN. width = 100;
BTN. Height = 100;
BTN. content = "login ";
This. content = BTN;
These lines of code are simple. A new button object is added to the content of the current object to an instance of the button.
Simplest set item
<ComboBox Height = "23" selectedindex = "0" horizontalalignment = "Left" margin = "127,12, 0, 0" name = "combox1" verticalignment = "TOP" width = "120">
<Comboboxitem> A </comboboxitem>
<Comboboxitem> B </comboboxitem>
</ComboBox>
This XAML describes a drop-down box. In fact, if you are familiar with Asp.net, the code will be clear at a glance, and The XAML will be easy to learn.
Now that we have talked about XAML and layout, we have a simple layout code for you to download.
Http://115.com/file/e7hbk0je
Of course, you can also write a complicated layout.
For example
<Comboboxitem>
<Rectangle fill = "red" width = "70" Height = "20"> </rectangle>
</Comboboxitem>
You can also write a rectangle in the Set item, or other controls. You can also insert a rectangle to the image. The parsing capability of XAML is very powerful.
<Comboboxitem>
<Rectangle width = "70" Height = "20">
<Rectangle. Fill>
<Imagebrush imagesource = "E: \ learning Code \ WPF Code \ Test2 \ xaml1 \ ..jpg"> </imagebrush>
</Rectangle. Fill>
</Rectangle>
</Comboboxitem>
Now that we have talked about one set item, so many other set items are needed. Let's do it on your own...
XAML syntax, continue Parsing
1. What is a type converter?
<Button width = "100" Height = "100">
<Button. Background> Red </button. Background>
</Button> first, check the code. <button. background> Red </button. background>, why do I write a red file to parse the corresponding color, which is the credit of the type converter ..
VaR A = system. Windows. Media. colorconverter. convertfromstring ("# ffcccccc ");
System. Windows. MessageBox. Show (A. tostring ());
VaR B = new system. Windows. Media. brushconverter (). convertfromstring ("red ");
System. Windows. MessageBox. Show (B. tostring ());
Look at this piece of code. This is the type converter in WPF. You can convert red to the argb value. The XAML of WPF also converts the value to the color through the converter.
2. Reference Value and tag extension.
Let's take a look at the reference value, how to define and reference it ..
Define resources on the grid,
<Window. Resources>
<Solidcolorbrush X: Key = "backgroundbrush"> yellow </solidcolorbrush>
<Solidcolorbrush X: Key = "borderbrush"> Red </solidcolorbrush>
</Window. Resources>
Two simple resource files are defined. In the following code, we will learn how to reference these defined resource files.
<Button background = "{staticresource backgroundbrush}" borderbrush = "{staticresource borderbrush}" width = "100" Height = "100"> </button>
This line of code is analyzed first. Background = "{staticresource backgroundbrush}" uses the static resource backgroundbrush.
You can write background = "{which will prompt you to use, static resources, bindings, dynamic resources, and other options. We choose to use static resources, then we can leave a space to write down the key of the resource you want to use .}"
Here, let's talk about our powerful Editor, Visual Studio. You can use the document outline to easily see your XAML hierarchy from the tree structure and the XAML syntax for each line, the rendered style. These functions are very powerful ..
3. Hide events and XAML code,
Like Asp.net, WPF is writing code directly on the UI. This is the case for hiding WPF code without changing it ..
<Button width = "100" Height = "100" Click = "button_click" content = "close"> </button>
<X: code>
<! [CDATA [
Void button_click (Object sender, routedeventargs E)
{
This. Close ();
}
]>
</X: code>
The hidden code is written below the button. debugging is not supported. I cannot use the prompt in the flagship version of vs2010. In general, the WPF code hiding function is very bad.
In fact, with the front-end code, you can implement the front-end code to call the back-end code and the back-end code to call the front-end code, which is very simple ..
This chapter has been written so much that it is almost New Year's Eve. Many things come to summarize this chapter. First, I learned about the composition of the XAML syntax and the binding of collection items, and complex rendering, followed by Type converters, resources, and code embedding ..
The next chapter describes the layout and resource style.
Not yet to be continued ....