WPF的XAML

Posted on 2013-08-19 15:49  roseworld  阅读(359)  评论(0)    收藏  举报

第一次看WPF程序,程序就两部分构成,一部分是XAML文件(XAML语言),另一部分是CS文件(是C#开发语言)。

初次看到XAML,真的很像XML语言,当然用了之后才发现,这个语言类似于布局控制和界面呈现用的,而且很方便。不像以前那个c#,老是要new一个对象出来,让后要show方法。

用一个

<Grid>    

</Grid>

就表示一个布局表格。

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30*" />
            <ColumnDefinition Width="70*" />
        </Grid.ColumnDefinitions>
    </Grid>

表示一个布局二列的表格。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*" />
            <RowDefinition Height="50*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30*" />
            <ColumnDefinition Width="70*" />
        </Grid.ColumnDefinitions>
    </Grid>

表示一个二行二列的布局表格。
Grid表示一个元素,Grid.RowDefinitions表示一个元素的属性元素, Height则表示一元素属性。这听起来好像有点混乱。其实就是属性中包括元素集合,元素中包括属性集合。他们相互嵌套下去罢了。

<Button Content="Button" />

 

<Button>Button</Button>
<Button>
            <Button.Content>
                Button
            </Button.Content>
        </Button>

都是表示一个按钮,只是写法不一样罢了。

        <StackPanel>
            <Button>
                Button
            </Button>
        </StackPanel>
       <StackPanel>
            <StackPanel.Children>
                   <Button Content="Button"   />
            </StackPanel.Children>
        </StackPanel>

以上代码的效果一致。

<Window x:Class="WpfApplication1.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 >
            <Button>
                Button
            </Button>
        </StackPanel>
    </Grid>
</Window>

 x:Class="WpfApplication1.MainWindow":表示一个x下对Class下的隐藏类的调用。

代码同样可以写成,也就是说X只是一个代替符号。

<Window win:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <StackPanel >
            <Button>
                Button
            </Button>
        </StackPanel>
    </Grid>
</Window>

xmlns表示主命名空间wpf引用,而xmlns:x表示另一个XAML命名空间引用。类似于XML文件的做法。

x:Key:为 ResourceDictionary 中的每个资源设置一个唯一的键。在应用程序标记中
看到的所有 x: 用法中,x:Key 可能占到  90% 。
x:Class:向为  XAML  页提供代码隐藏的类指定 CLR 命名空间和类名。必须具有这样一
个类才能支持代码隐藏,也正是由于这个原因,即使没有资源,您也几乎总是会看到映射的
x:。
x:Name:处理对象元素后,为运行时代码中存在的实例指定运行时对象名称。在不支持
等效的  WPF  框架级Name 属性的情况下命名元素时,可以使用  x:Name 。某些动画方案中会
发生这种情况。
x:Static:启用一个获取静态值的值引用,该静态值只能是一个  XAML 可设置属性。 
x:Type:根据类型名称构造一个  Type 引用。它用于指定采用  Type  的属性
(Attribute),如  Style..::.TargetType ,不过在许多情况下属性  (Property) 本身具有字
符串到  Type 的转换功能,因此使用 x:Type 是可选的。

 

 明天再看看什么是路由事件再写吧。

 

 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3