文章出處

1.標記擴展

將一個對象的屬性值依賴在其他其他對象的某個屬性上

用法:標記屬性的一般用法是:Attribute = Value,使用標記拓展,Value字符串是由一對花括號及其括起來的內容組成,XAML編譯器會對這樣的內容作出解析、生成相應的對象

例如:

<Window x:Class="標記拓展.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Background="LightSlateGray">
            <TextBox Text="{Binding ElementName=slider1,Path=Value,Mode=OneWay}" Margin="5"></TextBox>
            <Slider Name="slider1" Margin="5"></Slider>
        </StackPanel>
    </Grid>
</Window>

 

TextBox的Value值依賴在Slider的Value上 

以上的寫法相當于:

<TextBox Margin="5">
                <TextBox.Text>
                    <Binding ElementName="slider1" Path="Value" Mode="OneWay"></Binding>
                </TextBox.Text>
</TextBox>

不這也寫的原因是,代碼量增加了、閱讀不方便

注意:并不是所有對象都可以用標記拓展的語法來寫,只有MarkupExtension類的派生類才能使用標記拓展的語法來創建對象

 

2.代碼后置Code-Behind

 將邏輯代碼與UI代碼分離,隱藏在UI代碼后面的形式

 可以使用x:Code標簽,把應該呆在后置代碼里的C#代碼搬到XAML文件來,例如:

<Window x:Class="代碼后置.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="button1" Click="button1_Click_1"></Button>
    </Grid>
    <x:Code>
        <![CDATA[
             private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("ABC");
        }
            ]]>
    </x:Code>
</Window>

 

 

 3.導入程序集和引用其中的命名空間

1)在XAML中引用命名空間的語法是:

xmlns:映射名=“clr-namespace:類庫中名稱空間的名字;assembly=類庫文件名”

比如類庫程序集名為:MyLibrary.dll,其中包括Common和Controls兩個名稱空間,在XAML中引用會是:

xmlns:common=“clr-namespace:Common;assembly=MyLibrary” 

xmlns:controls=“clr-namespace:Controls;assembly=MyLibrary” 

 2)使用命名空間里的類:

<映射名:類名>...</映射名:類名>

比如:

<common:MessagePanel x:Name="window1" />

 

4.注釋

語法:<!--被注釋的內容-->

注意:

1)只能出現在開始標簽和結束標簽之間

2)不能注釋標簽的Attribute

3)注釋不能嵌套

 

 

 

 

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()