博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.6 wpf标记扩展
阅读量:4702 次
发布时间:2019-06-09

本文共 2904 字,大约阅读时间需要 9 分钟。

1.什么是标记扩展?为什么要有标记扩展?

标记扩展是扩展xmal的表达能力

为了克服现存的类型转换机制存在的

 

常用的标记扩展有如下:

x:Array 代表一个.net数组,它的子元素都是数组元素.它必须和x:Type一起使用,用于定义数组类型

x:null 表示空引用

x:static 在过程式代码中定义的任何一个静态属性、常量和枚举

x:type 表示system.Type的一个实例,就像C#中的typeof

注:标记扩展是有Extension的,可以省略不写

标记扩展的语法是 Attribute={} 是花括号

1.staticExtension

<StackPanel Background="{x:Static Brushes.AliceBlue}" />

 

2.ArrayExtension StaticResourceExtension

<Window.Resources>

        <x:ArrayExtension x:Key="listitemboxs" Type="TextBlock">
            <TextBlock Text=" one"></TextBlock>
            <TextBlock Text=" two"></TextBlock>
            <TextBlock Text=" three"></TextBlock>
        </x:ArrayExtension>
</Window.Resources>
<StackPanel>
         <ListBox ItemsSource="{
StaticResourceExtension listitemboxs}"></ListBox>
</StackPanel>

<Window x:Class="WpfApplication1.Window3"

        xmlns=""
        xmlns:x=""
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window3" Height="300" Width="300" WindowStartupLocation="CenterScreen">    
    <Window.Resources>
        <sys:String x:Key="stringHell0">Hello WPF</sys:String>
    </Window.Resources>
    <Grid>
        <TextBlock Height="24" Width="100" Text="{
StaticResource ResourceKey=stringHell0}"></TextBlock>
    </Grid>
</Window>

 

3.NullExtension

<StackPanel Background="{x:NullExtension}"/>

 

4.标记扩展有2种使用方式

<StackPanel Background="{x:NullExtension}"/>

<StackPanel>

        <StackPanel.Background>
            <x:Static Member="Brushes.AliceBlue"></x:Static>
        </StackPanel.Background>
 </StackPanel>

以上两种方式显示得到一样的效果,但他们的内部实现机制是不一样的

第一种:

  1. StaticExtension staticExtension = new StaticExtension("Brushes.AliceBlue");  
  2. StackPanel stackPanel = new StackPanel();      
  3. stackPanel.Background = staticExtension.ProvideValue(serviceProvider) as Brush;  
  4. AddChild(stackPanel);  

第二种:

  1. StaticExtension staticExtension = new StaticExtension();  
  2. staticExtension.Member = "Brushes.AliceBlue";  
  3. StackPanel stackPanel = new StackPanel();      
  4. stackPanel.Background = staticExtension.ProvideValue(serviceProvider) as Brush;  
  5. AddChild(stackPanel); 

这两种使用方式是一样的。但它们的内部实现机制却不同,Xaml解析器解析成的伪代码分别为:

第一种:

  1. StaticExtension staticExtension = new StaticExtension("Brushes.AliceBlue");  
  2. StackPanel stackPanel = new StackPanel();      
  3. stackPanel.Background = staticExtension.ProvideValue(serviceProvider) as Brush;  
  4. AddChild(stackPanel); 

第二种:

  1. StaticExtension staticExtension = new StaticExtension();  
  2. staticExtension.Member = "Brushes.AliceBlue";  
  3. StackPanel stackPanel = new StackPanel();      
  4. stackPanel.Background = staticExtension.ProvideValue(serviceProvider) as Brush;  
  5. AddChild(stackPanel); 

 实战 :拖动slider,textblock里的值变化

  <Grid Margin="4">

        <Grid.RowDefinitions>
            <RowDefinition Height="24"></RowDefinition>
            <RowDefinition Height="4"></RowDefinition>
            <RowDefinition Height="24"></RowDefinition>
        </Grid.RowDefinitions>总共有3行row
        <TextBlock x:Name="tb" Background="White" Text="{Binding ElementName=sid,Path=Value}"></TextBlock>
        <Slider Name="sid" Grid.Row="2" Value="50" Maximum="100" Minimum="0"></Slider>这里的Grid.Rows是指显示在Grid里的第2行
    </Grid>

注:绿色表示没看明白,日后要修改注的

转载于:https://www.cnblogs.com/meimao5211/p/3374207.html

你可能感兴趣的文章
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>