漫谈Silverlight(3)扩展DependencyObject像Html那样使用Attribute

本文介绍如何为Silverlight中的DependencyObject添加自定义属性,利用附加属性和扩展方法实现类似HTML属性的功能,包括设置、获取及检查属性的存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


======================================================
注:本文源代码点此下载
======================================================

您是否会觉得html中可以自由添加的属性很方便,对于每个html元素我们可以方便的将自己的属性加在元素上,而对于silverlight我们只能中规中矩的使用对象本身的属性,下面我们来尝试为dependencyobject添加类似的setattribute和getattribute方法。

为了完成这一目标,我们利用两个知识点:附加属性和扩展方法。大致的思路是这样的,为dependencyobject添加一个attributes的附加集合属性,然后使用扩展方法控制集合的添加删除等操作,原理比较简单,下面是实现,首先是附加属性,构建attributeservice类为dependencyobject添加附加属性attributes:

1: public class attributeservice

2: {

3:public static readonly dependencyproperty attributesproperty = dependencyproperty.registerattached(

4:"attributes",

5:typeof(attributedictionary),

6:typeof(dependencyobject),

7:new propertymetadata(null));

8:public static void setattributes(dependencyobject obj, attributedictionary attrcollection)

9:{

10:obj.setvalue(attributesproperty, attrcollection);

11:}

12:public static attributedictionary getattributes(dependencyobject obj)

13:{

14:return (attributedictionary)obj.getvalue(attributesproperty);

15:}

16: }

这里的attributedictionary是一个空实现:

1: public class attributedictionary : dictionarystring, object>

2: {

3: }

为什么我没有直接用dictionary来实现?其实没什么,只是为了在xaml中使用更加方便,不过这里要顺便说说扩展属性的默认值,在silverlight中大多数的扩展属性都是值类型,比如最常见的grid.column和grid.row等等,silverlight为我们存储扩展属性的默认值,如果是值类型,对于每个使用扩展属性的对象来说都有各自的值,即使这些值是一样的,但是如果扩展属性是引用类型就不一样了,所有的对象将共享同一个默认值,除非使用setvalue设置成新的值,但是我们在getvalue获得这个扩展属性的值的时候并没有办法知道这个值是默认值还是在跟其他对象共享这个值,这一点尤其需要注意。下面让我们来看看扩展的实现:

1: public static class dependencyobjectextension

2: {

3:public static void setattribute(this dependencyobject obj, string name, object value)

4:{

5:var attrcollection = attributeservice.getattributes(obj);

6:if (attrcollection == null)

7:{

8:attrcollection = new attributedictionary();

9:attributeservice.setattributes(obj, attrcollection);

10:}

11:if (value == null)

12:{

13:if (attrcollection.containskey(name))

14:attrcollection.remove(name);

15:}

16:else

17:{

18:if (attrcollection.containskey(name))

19:attrcollection[name] = value;

20:else

21:attrcollection.add(name, value);

22:}

23:}

24:public static object getattribute(this dependencyobject obj, string name)

25:{

26:var attrcollection = attributeservice.getattributes(obj);

27:if (attrcollection == null)

28:return null;

29:if (attrcollection.containskey(name))

30:return attrcollection[name];

31:return null;

32:}

33:public static bool hasattribute(this dependencyobject obj, string name)

34:{

35:var attrcollection = attributeservice.getattributes(obj);

36:if (attrcollection == null)

37:return false;

38:return attrcollection.containskey(name);

39:}

40: }

上面的代码扩展了dependencyobject为其添加了三个方法,在这三个方法中利用attributeservice的getattributes方法获得attributedictionary,注意getattribute方法中的实现,当获得的attributedictionary为null时用attributeservice的setattributes方法为传入的dependencyobject分配一个新的attributedictionary,如果我们在注册扩展属性的时候指定的默认值不是null,而是一个空的attributedictionary,那么所有被扩展的dependencyobject将使用同一个集合,显然我们不想要这样的结果。下面来看看如何使用,先看c#中如何使用:

1: private void usercontrol_loaded(object sender, routedeventargs e)

2: {

3:this.layoutroot.setattribute("attrname1", "in c#");

4: }

比较简单,再来看看在xaml中如何使用:

1: grid x:name="layoutroot" background="white">

2:local:attributeservice.attributes>

3:local:attributedictionary>

4:sys:string x:key="attrname2">in xamlsys:string>

5:local:attributedictionary>

6:local:attributeservice.attributes>

7: grid>

8:

虽然我们无法像html那样这样写:

1: grid x:name="layoutroot" background="white" attrname2="in xaml">

但是上面的写法也不算很丑陋了,并且似乎在silverlight的xaml中无法直接使用泛型,所以我用了attributedictionary的封装,如果高人知道如何在xaml中定义泛型,请指点。我们还是回到xaml中的代码,我之所以可以在attributedictionary下使用x:key得益于silverlight4,在之前的版本中,只有resourcedictionary中才能使用x:key,这样看起来是否还算舒服呢,是不是有点像frameworkelement.resources属性,最后再奉上一个valueconverter:

1: public class attributevalueconverter : ivalueconverter

2: {

3:public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)

4:{

5:var attrname = system.convert.tostring(parameter);

6:if (value is dependencyobject)

7:{

8:var obj = value as dependencyobject;

9:return obj.getattribute(attrname);

10:}

11:return null;

12:}

13:public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)

14:{

15:throw new notimplementedexception();

16:}

17: }

很遗憾,没办法双向绑定,大家可以根据这个思路继续扩展下去,使用如下:

1: grid x:name="layoutroot" background="white">

2:grid.resources>

3:local:attributevalueconverter x:key="attrconverter">local:attributevalueconverter>

4:grid.resources>

5:local:attributeservice.attributes>

6:local:attributedictionary>

7:sys:string x:key="attrname2">in xamlsys:string>

8:solidcolorbrush color="blue" x:key="blue">solidcolorbrush>

9:local:attributedictionary>

10:local:attributeservice.attributes>

11:button foreground="{binding elementname=layoutroot, converterparameter=blue, converter={staticresource attrconverter}}"

12:click="button_click" content="hello silverlight">button>

13: grid>

button的前景色被设置成了layoutroot的名为blue的attribute所指向的solidcolorbrush。希望本文对您有帮助。

完整代码下载


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值