zoukankan      html  css  js  c++  java
  • XAML-1

    1.XAML

    Extension Application Marked Language,是WPF技术中专门用来设计UI的语言。XAML是从XML派生出来的,是一种声明式语言,当你看到一个标签,就是声明了一个对象

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

    XAML语言有一个功能就是可以在XML标签上使用xmlns特征来定义命名空间,xmlns也是xml-namespace的缩写。

    xmlns特征语法格式:xmlns[:可选的映射前缀]="命名空间"。如果没有写可选映射前缀意味着所有来自这个命名空间的标签前都不用加标签,而这个没有映射前缀的命名空间称为"默认名称空间"(默认名称空间只能一个,而且应该选择其元素被最频繁使用的命名空间来充当默认名称空间)。

    x:Class="WPF.MainWindow"这个Attribute的作用是当XAML解析器把所包含它的标签解析成C#类时,这个类的类名是什么

    2.xaml引用外来程序集

    在C#中想引用System.Windows.Controls的Button类,需通过添加引用方式把包含System.Windows.Controls的程序集PresentationFrameWork添加到项目中,然后在C#代码顶部写上一句:System.Windows.Controls.XAML也是一样的,先添加对程序集的引用,再在根元素的起始标签写上一句:xmls:c="clrs-namespace:System.Windows.Controls;Assembly=PresentationFrameWork".所以使用button应该为<c:button></c:button>

    二.XAML语法

    XAML使用标签定义UI元素,每个标签对应NET Framework类库中的一个控件类,通过设置标签的Attribute可以对对应控件类的Property赋值.xaml使用树形逻辑结构来表示UI,同一个看上去一样的UI布局,XAML代码不是唯一的

    1.xaml对象属性赋值

    两种方式:

    • 使用字符串赋值,即标签属性Attribute为对象属性赋值
    • 使用属性标签赋值。xaml为属性赋值,大多数情况都是每个属性值都是为属性生成一个对象

    (1.)使用标签属性Attribute为对象属性赋值

    <Rectangle Width="100" Height="50" Fill="Blue"></Rectangle>

    通过Attribute=Value赋值,由于XAML语言限制,value只能是字符串。因此有时出现2中情况:A.一个类使用XAML声明并允许它的Attribute与Property互相映射,该类必须为这些Property准备转换机制;B.Value字符串格式的限制,此时可以用属性标签

    A.一个类使用XAML声明并允许它的Attribute与Property互相映射,该类必须为这些Property准备转换机制。

    exp:
    有个类
     public class Human
        {
            public string Name { get; set; }
            public Human Child { get; set; }
        }
    xaml文件:
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        
        <Window.Resources>
            <local:Human x:Key="Human" Child="ABC"></local:Human>
        </Window.Resources>
        <Button x:Name="button" Click="button_Click" Width="50" Height="20" Background="Blue"></Button>
        
    </Window>
    按钮事件:
    private void button_Click(object sender, RoutedEventArgs e)
            {
                Human h = (Human)this.FindResource("Human");
                MessageBox.Show(h.Child.Name);
            }

    编译成功,但运行失败。Human的Child是Human实例,XAML代码中ABC是字符串,xaml编译时不知道如何将字符串转换为Human对象。解决办法使用TypeConverter

    和TypeConverterAttribute:

    //首先重写一个类派生于TypeConverter
      public class StringToHumanTypeConverter : TypeConverter
        {
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                if (value is string)
                {
                    Human h = new Human();
                    h.Name = value as string;
                    return h;
                }
                return base.ConvertFrom(context, culture, value);
            }
        }
    //然后用TypeConverterAttribute将这个类与Human进行标明
    [TypeConverterAttribute(typeof(StringToHumanTypeConverter))]
        public class Human
        {
            public string Name { get; set; }
            public Human Child { get; set; }
        }

    对于普通的属性赋值字符串,XAML处理器会根据属性的类型决定是否需要执行对字符串的转化。如果属性的类型不是字符串,那么XAML处理器会调用相应的转化逻辑。如对于枚举类型的属性,XAML处理器将通过Enum的Parse方法得到相应类型的数值。而对于自定义类型,XAML会根据该自定义类型声明或属性声明上所标明的TypeConverter将字符串转换为该自定义类型。

    (2.)属性标签

    <className>
      <className.Property>
         <!--以对象形式为属性赋值-->
      </className.Property>
    </className>

    exp:
    <Rectangle x:Name="rectangle" Width="100" Height="20">
            <Rectangle.Fill>
                <SolidColorBrush Color="Blue"></SolidColorBrush>
            </Rectangle.Fill>
    </Rectangle>

    2.标记拓展

    WPF允许一个对象的属性值依赖于其他对象的属性,此时这个属性赋值需要用到标记拓展

    exp:TextBox的内容依赖于Slider的Value值

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel Background="LightSlateGray">
            <TextBox Text="{Binding ElementName=silder,Path=Value,Mode=OneWay}" Margin="5"></TextBox>
            <Slider x:Name="silder" Margin="5"></Slider> 
        </StackPanel>
    </Window>

    其中Text="{Binding ElementName=silder,Path=Value,Mode=OneWay}"即标记拓展,编译器解析到这句代码是将此解析成Binding实例对象。并不是所有对象都能使用

    标记拓展来书写,只有MarkupExtension的派生类(直接或间接,都以Extension为后缀)才可以。MarkupExstension的直接派生类:

    System.Object
      System.Windows.Markup.MarkupExtension
        System.Activities.Presentation.CachedResourceDictionaryExtension
        System.Activities.XamlIntegration.DynamicUpdateMapExtension
        System.Activities.XamlIntegration.PropertyReferenceExtension<T>
        System.ServiceModel.EndpointIdentityExtension
        System.ServiceModel.XamlIntegration.SpnEndpointIdentityExtension
        System.ServiceModel.XamlIntegration.UpnEndpointIdentityExtension
        System.ServiceModel.XamlIntegration.XPathMessageContextMarkupExtension
        System.Windows.ColorConvertedBitmapExtension
        System.Windows.Data.BindingBase
        System.Windows.Data.RelativeSource
        System.Windows.DynamicResourceExtension
        System.Windows.Markup.ArrayExtension
        System.Windows.Markup.NullExtension
        System.Windows.Markup.Reference
        System.Windows.Markup.StaticExtension
        System.Windows.Markup.TypeExtension
        System.Windows.ResourceKey
        System.Windows.StaticResourceExtension
        System.Windows.TemplateBindingExtension
        System.Windows.ThemeDictionaryExtension

    3.导入程序集与引用其中的名称空间

    xmlns:映射名="clr-namespace:名称空间;assembly=程序集名"

    exp:xmlns:sys="clr-namespace:System;assembly=mscorlib"

    4.注释:<!--注释掉的内容-->

  • 相关阅读:
    Angular 组件通信的三种方式
    Vue 之keep-alive的使用,实现页面缓存
    Angular Service设计理念及使用
    Git提交规范
    angular的生命周期
    CPU 是如何认识和执行代码的
    Ubuntu 常用软件
    UltraSQL / sqlserver-kit (SQLServer DBA )高手
    便宜的网站模板下载
    Audio over Bluetooth: most detailed information about profiles, codecs, and devices
  • 原文地址:https://www.cnblogs.com/come-on-come-on/p/4454814.html
Copyright © 2011-2022 走看看