标记属性
C#里的属性标签,现在还是不大明白,感觉上像是对某个方法的实现,或者是说有点像扩展方法。
使用步骤:
①自己定义的类继承一个预先会被调用的类
②用[] 的方式将消费者与刚自定义出来的这个类挂接起来
下面用一个WPF小实例说明下:
1 <Window x:Class="XamlStudyDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:local="clr-namespace:XamlStudyDemo"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 Title="MainWindow" Height="350" Width="525">
6
7 <Window.Resources>
8 <local:MyClass x:Key="demo" Name="老王" Age="29" Child="小王" />
9 </Window.Resources>
10
11
12 <Grid>
13
14 <Button Width="60" Height="29" Click="Button_Click"></Button>
15 </Grid>
16 </Window>
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:local="clr-namespace:XamlStudyDemo"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 Title="MainWindow" Height="350" Width="525">
6
7 <Window.Resources>
8 <local:MyClass x:Key="demo" Name="老王" Age="29" Child="小王" />
9 </Window.Resources>
10
11
12 <Grid>
13
14 <Button Width="60" Height="29" Click="Button_Click"></Button>
15 </Grid>
16 </Window>
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.ComponentModel;
15
16 namespace XamlStudyDemo
17 {
18 /// <summary>
19 /// MainWindow.xaml 的交互逻辑
20 /// </summary>
21 public partial class MainWindow : Window
22 {
23 public MainWindow()
24 {
25 InitializeComponent();
26 }
27
28 private void Button_Click(object sender, RoutedEventArgs e)
29 {
30 MyClass demo = this.FindResource("demo") as MyClass;
31 if (demo!=null)
32 {
33 MessageBox.Show(demo.Child.Name);
34 }
35
36 }
37 }
38
39 /// <summary>
40 /// 给属性标记 作者:王超
41 /// </summary>
42 [TypeConverterAttribute(typeof(CurrConverter))]
43 public class MyClass
44 {
45 public string Name { get; set; }
46 public string Age { get; set; }
47 public MyClass Child { get; set; }
48 }
49
50 /// <summary>
51 /// 定制类型转换器 作者:王超
52 /// </summary>
53 public class CurrConverter : TypeConverter
54 {
55 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
56 {
57 string name = value.ToString();
58 MyClass myclass = new MyClass();
59 myclass.Name = name;
60 return myclass;
61 }
62 }
63
64
65 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.ComponentModel;
15
16 namespace XamlStudyDemo
17 {
18 /// <summary>
19 /// MainWindow.xaml 的交互逻辑
20 /// </summary>
21 public partial class MainWindow : Window
22 {
23 public MainWindow()
24 {
25 InitializeComponent();
26 }
27
28 private void Button_Click(object sender, RoutedEventArgs e)
29 {
30 MyClass demo = this.FindResource("demo") as MyClass;
31 if (demo!=null)
32 {
33 MessageBox.Show(demo.Child.Name);
34 }
35
36 }
37 }
38
39 /// <summary>
40 /// 给属性标记 作者:王超
41 /// </summary>
42 [TypeConverterAttribute(typeof(CurrConverter))]
43 public class MyClass
44 {
45 public string Name { get; set; }
46 public string Age { get; set; }
47 public MyClass Child { get; set; }
48 }
49
50 /// <summary>
51 /// 定制类型转换器 作者:王超
52 /// </summary>
53 public class CurrConverter : TypeConverter
54 {
55 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
56 {
57 string name = value.ToString();
58 MyClass myclass = new MyClass();
59 myclass.Name = name;
60 return myclass;
61 }
62 }
63
64
65 }