虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > WindowsPhone/WindowsMobile > Windows phone 7之依赖属性(Dependency Properties)

Windows phone 7之依赖属性(Dependency Properties)
类别:WindowsPhone/WindowsMobile   作者:码皇   来源:互联网   点击:

依赖属性(Dependency Properties)如果你要创建一个自定义控件类,并为这个类定义新的属性,如果你想给这些属性设置Style,或者你想通过给那些属性设置数据绑定(Binding),或者想对这些属性使用动画,那么你需要

依赖属性(Dependency Properties)
如果你要创建一个自定义控件类,并为这个类定义新的属性,如果你想给这些属性设置Style,或者你想通过给那些属性设置数据绑定(Binding),或者想对这些属性使用动画,那么你需要将这些属性设置成依赖属性(Dependency Properties)。
下面我们先看一个简单了例子,自定义一个类,继承自Button,也就是我们自己做一个Button,并为之定义两个属性Color1和Color2,代码如下
public class MyButton : Button
    {
        public MyButton()
        {
           
        }

        private Color _forceColor;
        public Color ForceColor
        {
            get
            {
                return _forceColor;
            }
            set
            {
                _forceColor = value;
                this.Foreground = new SolidColorBrush() { Color = _forceColor};
            }
        }

        private Color _backColor;
        public Color BackColor
        {
            get
            {
                return _backColor;
            }
            set
            {
                _backColor = value;
                this.Background = new SolidColorBrush() { Color = _backColor };
            }
        }
    }
上面定义一个Button,声明两个属性Color1和Color2,为Color1属性Set值时,同时将值赋给前景色Foreground,为Color2属性Set值时,同时将值赋给背景色Background,
在MainPage中添当前项目的加命名空间xmlns:local="clr-namespace:DependencyProperties"
在页面中添加一个MyButton,直接枚举设置ForceColor和BackColor:
<local:MyButton BackColor="Red" ForceColor="Black" Content="MyButton1"></local:MyButton>
添加资源
<Color x:Key="MyForceColor">Red</Color>
<Color x:Key="MyBackColor">White</Color>
再在页面中添加一个MyButton,设置ForceColor和BackColor值为上面定义的资源
<local:MyButton BackColor="{StaticResource MyBackColor}" ForceColor="{StaticResource MyForceColor}" Content="MyButton2"></local:MyButton>
现在运行一下程序,效果如下


 
上面是预料中的结果,现在我们用Style设置ForceColor和BackColor的值,定义资源

上面是我定义资源时,代码编辑器中的效果,看到有错误提示,看看错误窗口

未将对象引用设置到对象的实例,这事怎么回事呢,先不管他,
在页面中添加一个MyButton,并使用上面定义的Style
<local:MyButton Style="{StaticResource MyStyle}" Content="MyButton3"></local:MyButton>
运行一下,看看

调试也是通不过,为什么呢,这就是我之前说的,要想为自定义的属性使用Style,那么就必须将之设置为DependencyProperty
DependencyProperty的定义格式为
 
 

 

public static readonly DependencyProperty 变量名=
DependencyProperty.Register("属性名",
typeof(属性类型),
typeof(所属类的类型),
new PropertyMetadata(默认值, 值变化时触发的方法));
现在我们修改之前的代码,将ForceColor和BackColor设置为DependencyProperty,修改后的MyButton类如下
public class MyButton : Button
    {
        public MyButton()
        {

        }

        public static readonly DependencyProperty ForceColorProperty =
            DependencyProperty.Register("ForceColor",
            typeof(Color),
            typeof(MyButton),
            new PropertyMetadata(Colors.Black, OnColorChanged));

        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor",
            typeof(Color),
            typeof(MyButton),
            new PropertyMetadata(Colors.White, OnColorChanged));

        public Color ForceColor
        {
            set { SetValue(ForceColorProperty, value); }
            get { return (Color)GetValue(ForceColorProperty); }
        }
        public Color BackColor
        {
            set { SetValue(BackColorProperty, value); }
            get { return (Color)GetValue(BackColorProperty); }
        }

        static void OnColorChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            var btn = obj as MyButton;
            if (args.Property == ForceColorProperty)
            {
                btn.Foreground = new SolidColorBrush() { Color = (Color)args.NewValue };
            }
            if (args.Property == BackColorProperty)
            {
                btn.Background = new SolidColorBrush() { Color = (Color)args.NewValue };
            }
        }
    }

  现在看看之前定义的Style

 已经不报错了,现在运行程序,效果如下

 现在我们知道DependencyProperty的重要性了吧
 
再给初学者说明点问题,明白人绕过
上面的例子中定义DependencyProperty是使用的是static和readonly,这就是我们不能直接用"="为其复制,而且是静态变量,而非实例变量。所有MyButton公用的属性,而且上面OnColorChanged也是static,全是静态的,那么我们猜想,在设置他们的值是,那不就是改变了所有MyButton的属性了吗,那可绝对不是我们想要的结果,那么们看一看,是不是事情是不是我们想的那样糟糕 www.2cto.com
再次添加一个Style,并添加一个MyButton,使用次Style
<Style x:Key="MyStyle2" TargetType="local:MyButton">
            <Setter Property="BackColor" Value="Yellow"></Setter>
            <Setter Property="ForceColor" Value="Black"></Setter>
</Style>
<local:MyButton Style="{StaticResource MyStyle2}" Content="MyButton4"></local:MyButton>
运行一下,看看效果

这不是我们猜想的坏的结果,而是一个令人激动地,满意的结果,为什么会这样,关键就在OnColorChanged参数和方法体上,耐心点,
 
 

再看看完整的OnColorChanged
static void OnColorChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            var btn = obj as MyButton;
            if (args.Property == ForceColorProperty)
            {
                btn.Foreground = new SolidColorBrush() { Color = (Color)args.NewValue };
            }
            if (args.Property == BackColorProperty)
            {
                btn.Background = new SolidColorBrush() { Color = (Color)args.NewValue };
            }
        }
我们看到,有两个参数,第一个参数就是当前,使用属性的实例对象MyButton,第二个参数是但前设置的值
我们将之获取,然和设置相应的属性,就可以了,这样虽然是静态变量,也不会不会影响到其他的实例。
在看看Button的Foreground属性,官方定义的是public Brush Foreground { get; set; },所以他是一个实例属性,而非静态属性,所以你想用MyButton.Foreground是通不过的,如果你想讲自己顶一个属性用到基础属性上,根本不会出错。
如果你本来就没有上面的错误猜测,就当我说了一个笑话吧

 

作者 董贺超
相关热词搜索: