Fun with Silverlight2.0系列之三 -- Skinnable动态换肤效果
2008-10-11 11:41:49 来源:WEB开发网完成了这两点程序已经可以动态切换皮肤了,自动查找皮肤文件的功能其实可有可无,但是这样做可以省却维护配置文件的麻烦,
首先定义一个Skin的属性:
1namespace Skinnable
2{
3 [AttributeUsage(AttributeTargets.Class)]
4 public class SkinAttribute : Attribute
5 {
6 public string DisplayName { get; set; }
7 }
8}然后所有的皮肤样式都实现这个属性,比如:
1 [Skin(DisplayName = "Red")]
2 public partial class RedSkin : UserControl, ISkin
3 {
最后在Assembly里找到所有实现Skin属性的皮肤,保存到List中做成画面上面的菜单
1internal List<SkinDefinition> AppSkins { get; private set; }
2
3public App()
4 {
5 this.AppSkins = new List<SkinDefinition>();
6 ScanAssemblyForSkins(Assembly.GetExecutingAssembly());
7 }
8
9 /// <summary>
10 /// Scans an assembly for skins
11 /// </summary>
12 /// <param name="asm"></param>
13 private void ScanAssemblyForSkins(Assembly asm)
14 {
15 if (asm != null)
16 {
17 LoadSkins(asm);
18 }
19 }
20
21
22 /// <summary>
23 /// Loads all the calculator skins that can be found inside the assembly
24 /// </summary>
25 /// <param name="asm"></param>
26 private void LoadSkins(Assembly asm)
27 {
28 // Look for all internal static classes in the assembly
29 var classTypes = from type in asm.GetTypes()
30 let attr = GetSkinAttribute(type)
31 where type.IsAbstract == false && type.IsClass == true && attr != null
32 select new { ClassType = type, SkinAttribute = attr };
33
34 foreach (var ct in classTypes)
35 {
36 string name = ct.SkinAttribute.DisplayName;
37 if (string.IsNullOrEmpty(name))
38 name = ct.ClassType.Name;
39
40 this.AppSkins.Add(new SkinDefinition(name, ct.ClassType));
41 }
42 }
43
44 /// <summary>
45 /// Finds the SkinAttribute if it is on the type
46 /// </summary>
47 /// <param name="member"></param>
48 /// <returns></returns>
49 static private SkinAttribute GetSkinAttribute(Type type)
50 {
51 return (from a in type.GetCustomAttributes(typeof(SkinAttribute), true)
52 select a as SkinAttribute).FirstOrDefault();
53 }
到这里动态换肤的程序就告一段落了,但现在有一个问题,那就是假如有两个皮肤文件A和B,A定义了Button的皮肤,B中没有定义,
那么A切换到B,Button如何回到原始的样子?如果不同的样式文件定义的控件不一致有什么好方法调整吗?
Tags:Fun with Silverlight
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
- ››silverlight2 游戏 1 你能坚持多少秒
- ››Silverlight开发实践--PicZoomShow
- ››Silverlight自定义控件开发 - 令人懊恼的OnApplyT...
- ››Silverlight 2 RTW中ToolTipService.ToolTip不继承...
更多精彩
赞助商链接