WPF 命名空间

1、项目默认创建之后会有一个本地的命名空间引入

xmlns:local="clr-namespace:studywpf"

2、引入命名空间的格式:

     

xmlns:前缀="clr-namespace:命名空间"

3、本地使用local 前缀、如果使用系统的则使用 sys前缀。引入示例之后就可以使用命名空间内的类创建对象和控件等等

4、基本引入示例:

<Window x:Class="studywpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys ="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:studywpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
       
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" 
                 x:Name="txtQuestion" Grid.Row="0"
                 TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"></TextBox>
        <Button VerticalAlignment="Top" HorizontalAlignment="Left"   Margin="10,0,0,20"
                Width="127" Height="23" x:Name="cmdAnswer" Click="cmdAnswer_Click" Grid.Row="1" Content="Ask thr Eight Ball"></Button>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer"
                 TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"
                 Grid.Row="2"></TextBox>
        <ListBox Grid.Row="2">
            <ListBoxItem>
                <sys:DateTime>10/11/2013</sys:DateTime>
            </ListBoxItem>
            <ListBoxItem>
                <sys:DateTime>10/11/2013</sys:DateTime>
            </ListBoxItem>
        </ListBox>

        <Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop Offset="0.00" Color="Red"/>
                    <GradientStop Offset="0.50" Color="Indigo" />
                    <GradientStop Offset="1.00" Color="Violet" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Grid.Background>

    </Grid>
</Window>

上例中: 引入sys前缀的datetime 命名空间:使用示例为:

  <ListBox Grid.Row="2">
            <ListBoxItem>
                <sys:DateTime>10/11/2013</sys:DateTime>
            </ListBoxItem>
            <ListBoxItem>
                <sys:DateTime>10/11/2013</sys:DateTime>
            </ListBoxItem>
        </ListBox>

猜你喜欢

转载自blog.csdn.net/qq_31319235/article/details/121611937
WPF