WEB开发网      婵犵數濞€濞佳囧磹婵犳艾鐤炬い鎰堕檮閸嬬喐銇勯弽銊с€掗梻鍕閺岋箑螣娓氼垱笑闂佽姘﹂褔婀佸┑鐘诧工妤犲憡绂嶉崜褏纾奸弶鍫涘妼缁楁岸鏌熷畡鐗堝殗闁诡喒鏅犲畷褰掝敃閵堝棙顔忔繝鐢靛仦閸ㄥ爼骞愰幘顔肩;闁规崘绉ぐ鎺撳亹闁绘垶锕╁Λ鍕⒑閹肩偛濡奸悗娑掓櫇缁顓兼径妯绘櫇闂佹寧绻傞弻濠囨晝閸屾稓鍘甸柣搴㈢⊕閿氶柣蹇ョ稻缁绘繃绻濋崘銊т紝闂佽鍨伴崯鏉戠暦閻旂⒈鏁傞柛鈾€鏅欑槐妯衡攽閻愬樊鍤熷┑顔藉劤铻為柛鏇ㄥ墯閸欏繘鏌嶉崫鍕櫣缂佲偓婢跺绠鹃柟瀛樼箘閿涘秵顨ラ悙顏勭伈闁诡喖缍婂畷鎯邦槻婵℃彃顭烽弻娑㈠Ω閵夈儺鍔夌紓浣稿€哥粔褰掑极閹剧粯鏅搁柨鐕傛嫹 ---闂傚倷鐒︾€笛兠洪埡鍛闁跨噦鎷�
开发学院图形图像Flash Silverlight自定义控件开发 - 令人懊恼的OnApplyT... 阅读

Silverlight自定义控件开发 - 令人懊恼的OnApplyTemplate问题

 2009-04-25 12:05:09 来源:WEB开发网 闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹闂傚倷绀侀幉锟犲垂閻㈠灚宕查柟鎵閸庡秵銇勯幒鎴濃偓鐢稿磻閹炬枼妲堟繛鍡楃С濞岊亞绱撻崒姘扁枌闁瑰嚖鎷�婵犵數濮幏鍐川椤撴繄鎹曢梻渚€娼уú銈吤洪妸鈺佺劦妞ゆ帊鑳堕埊鏇㈡煏閸モ晛浠х紒杈╁仱閺佹捇鏁撻敓锟�闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹  闂傚倷鑳舵灙缂佺粯顨呴埢宥夊即閵忕姵鐎梺缁樺姈椤愮厧鈽夊Ο閿嬬€婚梺褰掑亰閸撴稑鈻斿鑸碘拺闁告稑饪村▓鏃€绻涚仦鍌氬闁崇粯鎹囬獮瀣攽閹邦剚顔傛俊鐐€栧濠氬储瑜忛幉鎾晸閿燂拷
核心提示:继承自System.Windows.Controls.Control是不知什么时候调用OnApplyTemplate方法的,这就有可能给你制造麻烦,Silverlight自定义控件开发 - 令人懊恼的OnApplyTemplate问题,例如,开发了一个PathButton控件,让使用者可以设置Path的Data,///

继承自System.Windows.Controls.Control是不知什么时候调用OnApplyTemplate方法的,这就有可能给你制造麻烦,例如,开发了一个PathButton控件,XAML中定义一个了Path对象,然后定义了Data这个属性,让使用者可以设置Path的Data,

        /// <summary>
        /// Gets or sets the data.
        /// </summary>
        /// <value>The data.</value>
        public string Data
        {
            get { return (string)GetValue(DataProperty); }
            set
            {
                SetValue(DataProperty, value);

                PathElement.Data = Services.PathGeometryParser.Parse(value);
            }
        }

其它地方使用这个控件

PathButton bt = new PathButton();

bt.Data = "....";

NullReferenceException

解决的方法就是在PathButton的构造函数里就进行加载模板,然后调用ApplyTemplate方法。下面是我的实现方法,代码很简单

 1     /// <summary>
 2     /// XControl
 3     /// </summary>
 4     public class XControl : Control
 5     {
 6         /// <summary>
 7         /// True表示已经应用过模板
 8         /// </summary>
 9         protected bool _TemplateApplied;
10 
11         /// <summary>
12         /// 初始化组件
13         /// </summary>
14         protected virtual void InitializeComponent()
15         {
16             var objStyle = GetStyle(this.DefaultStyleKey as Type);
17             if (objStyle != null)
18             {
19                 this.Style = objStyle;
20                 this.ApplyTemplate();
21             }
22         }
23         /// <summary>
24         /// 请重写此方法代替OnApplyTemplate
25         /// </summary>
26         protected virtual void OnApplyXTemplate()
27         {
28 
29         }
30         /// <summary>
31         /// 请重写OnApplyXTemplate方法代替
32         /// </summary>
33         public override sealed void OnApplyTemplate()
34         {
35             if (this._TemplateApplied)
36                 return;
37             this._TemplateApplied = true;
38             this.OnApplyXTemplate();
39         }
40 
41         #region Static Methods
42         /// <summary>
43         /// 样式字典集合
44         /// </summary>
45         private static System.Collections.Generic.Dictionary<Type, ResourceDictionary> _ResourceDictionarys =
46             new System.Collections.Generic.Dictionary<Type, ResourceDictionary>();
47         /// <summary>
48         /// 获取样式
49         /// </summary>
50         /// <param name="styleKey"></param>
51         /// <returns></returns>
52         static public Style GetStyle(Type styleKey)
53         {
54             var rd = GetResourceDictionary(styleKey);
55             if (rd != null)
56             {
57                 string strKey = styleKey.ToString();
58                 if (rd.Contains(strKey))
59                     return rd[strKey] as Style;
60             }
61             return null;
62         }
63         /// <summary>
64         /// 获取样式字典
65         /// </summary>
66         /// <param name="styleKey"></param>
67         /// <returns></returns>
68         static public ResourceDictionary GetResourceDictionary(Type styleKey)
69         {
70             if (_ResourceDictionarys.ContainsKey(styleKey))
71                 return _ResourceDictionarys[styleKey];
72 
73             string fullName = styleKey.Assembly.FullName;
74             string baseName = fullName.Substring(0, fullName.IndexOf(",")) + ".g";
75 
76             System.Resources.ResourceManager manager = new System.Resources.ResourceManager(baseName, styleKey.Assembly);
77             System.IO.UnmanagedMemoryStream stream = null;
78             try
79             {
80                 stream = manager.GetStream("themes/generic.xaml", System.Globalization.CultureInfo.CurrentUICulture);
81                 string strTemplate = new System.IO.StreamReader(stream).ReadToEnd();
82                 var rd = System.Windows.Markup.XamlReader.Load(strTemplate) as ResourceDictionary;
83                 _ResourceDictionarys.Add(styleKey, rd);
84                 return rd;
85             }
86             catch { }
87             finally
88             {
89                 stream.Dispose();
90             }
91             return null;
92         }
93         #endregion
94     }

1 2  下一页

Tags:Silverlight 定义 控件

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接