WPF学习系列012: 2.7.4 XAML 扩展部分

 

  •  
    1. 可以在 XAML 中使用任何类型的.NET对象(甚至是COM对象),也可以使用自己定义的对象,无论这些对象是不是与用户界面有关。对象必须以“友好声明”(declarative-friendly)的方式进行设计。
    2. 对于不是专门为 XAML 设计的程序集,仍然可以在 XAML 中使用。
    1. 当转换子元素时,任何一个有效的 XAML 解析器或者编译器必须遵循下面的规则:
      1. 如果该类型实现了 IList  接口,就为每个子元素调用 IList.Add
      2. 否则,如果该类型实现了 IDictionary 接口,就为每个子元素调用 IDictionary.Add,在该值的键和元素中使用 x:Key 的特性值
      3. 否则,如果父元素支持内容属性,而且子元素的类型与该内容属性是兼容的,就把子元素作为它的属性值
      4. 否则,如果子对象是普通文本,且有类型转换器将子对象转换为父类型(没有在父元素上设置属性),则把子元素作为类型转换器的输入,将输出作为父对象的实例
      5. 其它情况,则抛出一个错误
  • 2.7.4 XAML 扩展部分

    例如:

    mscorlib.dll 中的 .NET Framework API

    System.Collections.Hashtable h = new System.Collections.Hashtable();

    h.Add("key1", 7);

    h.Add("key2", 23);

    如果要在 XAML 中引用上面程序集中的 API,可以表示为:

    <collections:Hashtable

    xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"

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

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <sys:Int32 x:Key="key1">7</sys:Int32>

    <sys:Int32 x:Key="key2">23</sys:Int32>

    </collections:Hashtable>

    其中:clr-namespace标记允许直接在 XAML 中放入一个 .NET 命名空间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,可以使用 ed:Arc 控件来绘制圆弧进度条,并设置两端圆角。具体步骤如下: 1. 引入 ed:Arc 命名空间: ```xaml xmlns:ed="http://schemas.efxui.com/xaml" ``` 2. 在 XAML 中使用 ed:Arc 控件,并设置 StrokeStartLineCap 和 StrokeEndLineCap 属性为 Round: ```xaml <ed:Arc Stroke="Gray" StrokeThickness="10" StrokeStartLineCap="Round" StrokeEndLineCap="Round" ArcThickness="10" ArcStart="90" ArcEnd="{Binding Progress}" /> ``` 其中,StrokeStartLineCap 和 StrokeEndLineCap 属性设置线条端点的形状,ArcThickness 属性设置圆弧的宽度,ArcStart 和 ArcEnd 属性设置圆弧的起点和终点。 3. 在 ViewModel 中定义 Progress 属性,并使用 Timer 定时更新进度: ```csharp public class MainWindowViewModel : INotifyPropertyChanged { private int _progress; private Timer _timer; public int Progress { get => _progress; set { if (_progress != value) { _progress = value; OnPropertyChanged(nameof(Progress)); } } } public MainWindowViewModel() { _timer = new Timer(100); _timer.Elapsed += (sender, e) => { Progress += 5; if (Progress > 360) { Progress = 0; } }; _timer.Start(); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` 完整代码示例: MainWindow.xaml: ```xaml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ed="http://schemas.efxui.com/xaml" Title="MainWindow" Height="200" Width="200"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Text="Progress:" Margin="5" /> <ed:Arc Grid.Row="1" Stroke="Gray" StrokeThickness="10" StrokeStartLineCap="Round" StrokeEndLineCap="Round" ArcThickness="10" ArcStart="90" ArcEnd="{Binding Progress}" /> </Grid> </Window> ``` MainWindow.xaml.cs: ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); } } ``` MainWindowViewModel.cs: ```csharp public class MainWindowViewModel : INotifyPropertyChanged { private int _progress; private Timer _timer; public int Progress { get => _progress; set { if (_progress != value) { _progress = value; OnPropertyChanged(nameof(Progress)); } } } public MainWindowViewModel() { _timer = new Timer(100); _timer.Elapsed += (sender, e) => { Progress += 5; if (Progress > 360) { Progress = 0; } }; _timer.Start(); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值