且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

34.Silverlight中不得不了解使用的依赖属性

更新时间:2022-09-27 08:43:07

 Silverlight中我们经常使用自定义控件,并且在自定义控件中制作自定义属性,在项目中大量的创建这个自定义控件,每次都需要占用内存来创建这个 属性的默认值。可很多时候我们都只需要去使用这个属性的默认值就可以,并不用每次都使用这个属性。所以在Silverlight和WPF中引入了依赖属性 这个概念,以节约内存并且可以灵活的使用属性。

        首先我们了解一下Silverlight中有3种属性的创建方式:一、CLR自定义属性。二、依赖属性。三、附加属性。

        一、下面我们来看一下CLR自定义属性的创建方式如下,相信大家都创建过:

 


  1. private string _RectWidth; 
  2.  
  3.    public string RectWidth 
  4.    { 
  5.        get { return _RectWidth; } 
  6.        set { _RectWidth = value; } 
  7.    } 

        二、依赖属性是存储于基类DependencyObject中的一个键值配对字典中的。它并没有存储在所属的类中,所以不会每次都去重新创建一个属性默认值占用内存。下面我们来看看创建一个依赖属性的代码如下:

 


  1. #region 设置一个X坐标属性 
  2. public double X 
  3.     get { return (double)GetValue(XProperty); } 
  4.     set { SetValue(XProperty, value); } 
  5.  
  6. // 创建一个名为X的依赖属性,并且设置这个依赖属性X变化的时候,让rectangle1控件位置也变化。 
  7. public static readonly DependencyProperty XProperty = 
  8.     DependencyProperty.Register("X", typeof(double), typeof(Rectan), new PropertyMetadata(OnXChanged)); 
  9.  
  10. private static void OnXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
  11.     Rectan rec = d as Rectan; 
  12.     rec.rectangle1.SetValue(Canvas.LeftProperty, e.NewValue); 
  13. #endregion   

        注意在OnXChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)函数中处理在依赖属性变化时进行的操作。在本例中改变rectangle1矩形框的位置。注册好了依赖属性之后我们可以通过XAML方式和CS代码方 式来控制自定义控件类的依赖属性分别如下:

        XAML方式控制依赖属性:


<uc:Rectan X="15" Y="105" x:Name="Uil" Width="105" Height="45" HorizontalAlignment="Left" VerticalAlignment="Top"></uc:Rectan>

        CS代码控制依赖属性:

 


  1. this.Ikd.SetValue(Rectan.XProperty, 150.0); 
  2. this.Ikd.SetValue(Rectan.YProperty, 150.0); 
  3.  
  4. 在这里主要是通过DependencyProperty.Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);函数注册依赖属性的。在OnXChanged()函数中我们可以处理当属性值变化的时候需要进行的逻辑操作。 
  5.  
  6. // 摘要: 
  7. // 使用属性的指定属性名称、属性类型、所有者类型和属性元数据注册依赖项属性。 
  8. // 参数: 
  9. // name
  10. // 要注册的依赖项对象的名称。 
  11. // propertyType: 
  12. // 属性的类型。 
  13. // ownerType: 
  14. // 正注册依赖项对象的所有者类型。 
  15. // typeMetadata: 
  16. // 属性元数据实例。实例中可以包含一个 System.Windows.PropertyChangedCallback 实现引用。 
  17. // 返回结果: 
  18. // 一个依赖项对象标识符,应使用它在您的类中设置 public static readonly 字段的值。随后可以在您自己的代码以及任何第三方用户代码中使用该标识符在将来引用该依赖项属性,用于某些操作,比如以编程方式设置该属性的值,或是在代码中附加 
  19. // System.Windows.Data.Binding。 
  20.  
  21. public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata); 

        下面我们看注册依赖属性的Register函数的相关参数以及用法:

        三、附加属性是一种特殊的依赖属性,是全局依赖属性,它的注册方式是DependencyProperty.RegisterAttached();参数和依赖属性的注册参数相同。

        下面我们来看本实例中的完整的XAML代码和XAML.CS代码如下:

Rectan.xaml.cs

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12.  
  13. namespace SLDependencyProp 
  14. public partial class Rectan : UserControl 
  15. public Rectan() 
  16. InitializeComponent(); 
  17.  
  18. private string _RectWidth; 
  19.  
  20. public string RectWidth 
  21. get { return _RectWidth; } 
  22. set { _RectWidth = value; } 
  23.  
  24. #region 设置一个X坐标属性 
  25. public double X 
  26. get { return (double)GetValue(XProperty); } 
  27. set { SetValue(XProperty, value); } 
  28.  
  29. // 创建一个名为X的依赖属性,并且设置这个依赖属性X变化的时候,让rectangle1控件位置也变化。 
  30. public static readonly DependencyProperty XProperty = 
  31. DependencyProperty.Register("X", typeof(double), typeof(Rectan), new PropertyMetadata(OnXChanged)); 
  32.  
  33. private static void OnXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
  34. Rectan rec = d as Rectan; 
  35. rec.rectangle1.SetValue(Canvas.LeftProperty, e.NewValue); 
  36. #endregion 
  37.  
  38. #region 设置一个Y坐标属性 
  39. public double Y 
  40. get { return (double)GetValue(YProperty); } 
  41. set { SetValue(YProperty, value); } 
  42.  
  43. // 创建一个名为X的依赖属性,并且设置这个依赖属性X变化的时候,让rectangle1控件位置也变化。 
  44. public static readonly DependencyProperty YProperty = 
  45. DependencyProperty.Register("Y", typeof(double), typeof(Rectan), new PropertyMetadata(OnYChanged)); 
  46.  
  47. private static void OnYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
  48. Rectan rec = d as Rectan; 
  49. rec.rectangle1.SetValue(Canvas.TopProperty, e.NewValue); 
  50. #endregion 
  51.  
Rectan.xaml

  1. <UserControl x:Class="SLDependencyProp.Rectan" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6. mc:Ignorable="d" 
  7. d:DesignHeight="45" d:DesignWidth="105"
  8.  
  9. <Canvas x:Name="LayoutRoot" Background="White"
  10. <Rectangle Height="45" HorizontalAlignment="Left" RadiusX="15" RadiusY="15" Fill="BurlyWood" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="105" /> 
  11. </Canvas> 
  12. </UserControl> 
MainPage.xaml.cs

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12.  
  13. namespace SLDependencyProp 
  14. public partial class MainPage : UserControl 
  15. public MainPage() 
  16. InitializeComponent(); 
  17. this.Ikd.SetValue(Rectan.XProperty, 150.0); 
  18. this.Ikd.SetValue(Rectan.YProperty, 150.0); 
MainPage.xaml

  1. <UserControl x:Class="SLDependencyProp.MainPage" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6. xmlns:uc="clr-namespace:SLDependencyProp" 
  7. mc:Ignorable="d" 
  8. d:DesignHeight="300" d:DesignWidth="400"
  9.  
  10. <Grid x:Name="LayoutRoot" Background="White"
  11. <uc:Rectan X="15" Y="105" x:Name="Uil" Width="105" Height="45" HorizontalAlignment="Left" VerticalAlignment="Top"></uc:Rectan> 
  12. <uc:Rectan x:Name="Ikd" Width="105" Height="45" HorizontalAlignment="Left" VerticalAlignment="Top"></uc:Rectan> 
  13. <uc:Rectan Width="105" Height="45" HorizontalAlignment="Left" VerticalAlignment="Top"></uc:Rectan> 
  14. </Grid> 
  15. </UserControl> 

        本实例采用VS2010+Silverlight 4.0编写,如需源码请点击 SLDependencyProp.rar 下载。



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/822564