基于Cairngorm的Silverlight开发 - part3
生活随笔
收集整理的這篇文章主要介紹了
基于Cairngorm的Silverlight开发 - part3
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用ModelLocator來管理視圖
之前只是簡單的介紹了一下ModelLocator的用法,在這里要把模型和視圖結合起來,通過模型來來控制視圖。在Silverlight中我們可以看到所有新建立的xaml都是繼承自UserControl,所以在這里更新歡稱視圖為控件。至此給出一個項目的結構圖出來。這里我是習慣把從網上下載的第三方類庫放在項目中一個Lib的目錄下,如果有源碼的話直接加入解決方案中也是可以的。
所有的用戶控件都是創建在Controls目錄下。這里提到了創建用戶控件,所以就不得不提一下控件的DependencyProperty屬性,他是控件的一個靜態的屬性,主要用來做數據綁定。
為控件創建DependencyProperty屬性
創建了DependencyProperty后能更方便的和ModelLocator進行綁定。處理一些界面上的動畫效果也能更加的靈活。這里給出一個標準的代碼
?? ? ? ?//?Using?a?DependencyProperty?as?the?backing?store?for?TheName.??
????????//?This?enables?animation,?styling,?binding,?etc
????????public?static?readonly?DependencyProperty?TheNameProperty?=
????????????DependencyProperty.Register("TheName",
????????????????????????????????????????typeof(string),
????????????????????????????????????????typeof(Page),
????????????????????????????????????????new?PropertyMetadata("",
??????????????????????????????????????????new?PropertyChangedCallback(
????????????????????????????????????????????OnTheNameChanged)));
????????static?void?OnTheNameChanged(object?sender,
?????????????????????????????????????DependencyPropertyChangedEventArgs?args)
????????{
????????????//?Get?reference?to?self
????????????Page?source?=?(Page)sender;
????????????//?Add?Handling?Code
????????????string?newValue?=?(string)args.NewValue;
????????} 更多關于創建自定義用戶控件的請查看winter-cn?前輩的《Silverlight 2 Customized Control 開發》?,寫的非常的詳細。(我這里就不再去重復的發明輪子了)
創建一個會變色的控件
這里首先看Demo
學習是一個溫故知新的過程,之前我寫過一篇《動態創建Storyboard》這里就用上他再結合DependencyProperty做一個會變色的控件。
運用DependencyProperty結合Storyboard創建控件
?? ?public?partial?class?BackGorund?:?UserControl
????{
????????public?byte?R
????????{
????????????get?{?return?(byte)GetValue(RProperty);?}
????????????set?{?SetValue(RProperty,?value);?}
????????}
????????//?Using?a?DependencyProperty?as?the?backing?store?for?R.??
????????//?This?enables?animation,?styling,?binding,?etc
????????public?static?readonly?DependencyProperty?RProperty?=
????????????DependencyProperty.Register("R",
????????????????????????????????????????typeof(byte),
????????????????????????????????????????typeof(BackGorund),
????????????????????????????????????????new?PropertyMetadata((byte)0,
????????????????????????????????????????????new?PropertyChangedCallback(OnRChanged)));
????????static?void?OnRChanged(object?sender,?DependencyPropertyChangedEventArgs?args)
????????{
????????????//?Get?reference?to?self
????????????BackGorund?source?=?(BackGorund)sender;
????????????source.changeColor();
????????}
????????public?byte?G
????????{
????????????get?{?return?(byte)GetValue(GProperty);?}
????????????set?{?SetValue(GProperty,?value);?}
????????}
????????//?Using?a?DependencyProperty?as?the?backing?store?for?G.??
????????//?This?enables?animation,?styling,?binding,?etc
????????public?static?readonly?DependencyProperty?GProperty?=
????????????DependencyProperty.Register("G",
????????????????????????????????????????typeof(byte),
????????????????????????????????????????typeof(BackGorund),
????????????????????????????????????????new?PropertyMetadata((byte)0,
????????????????????????????????????????????new?PropertyChangedCallback(OnGChanged)));
????????static?void?OnGChanged(object?sender,?DependencyPropertyChangedEventArgs?args)
????????{
????????????//?Get?reference?to?self
????????????BackGorund?source?=?(BackGorund)sender;
????????????source.changeColor();
????????}
????????public?byte?B
????????{
????????????get?{?return?(byte)GetValue(BProperty);?}
????????????set?{?SetValue(BProperty,?value);?}
????????}
????????//?Using?a?DependencyProperty?as?the?backing?store?for?B.??
????????//?This?enables?animation,?styling,?binding,?etc
????????public?static?readonly?DependencyProperty?BProperty?=
????????????DependencyProperty.Register("B",
????????????????????????????????????????typeof(byte),
????????????????????????????????????????typeof(BackGorund),
????????????????????????????????????????new?PropertyMetadata((byte)0,
????????????????????????????????????????????new?PropertyChangedCallback(OnBChanged)));
????????static?void?OnBChanged(object?sender,?DependencyPropertyChangedEventArgs?args)
????????{
????????????//?Get?reference?to?self
????????????BackGorund?source?=?(BackGorund)sender;
????????????source.changeColor();
????????}
????????public?void?changeColor()
????????{
????????????colorAnim.To?=?Color.FromArgb(255,?R,?G,?B);
????????????storyboard.Begin();
????????}
????????private?ColorAnimation?colorAnim;
????????private?Storyboard?storyboard;
????????public?BackGorund()
????????{
????????????InitializeComponent();
????????????storyboard?=?new?Storyboard();
????????????Brush?br?=?this.LayoutRoot.Background;
????????????colorAnim?=?new?ColorAnimation();
????????????colorAnim.To?=?Color.FromArgb(255,?R,?G,?B);
????????????colorAnim.Duration?=?TimeSpan.FromSeconds(1);
????????????colorAnim.RepeatBehavior?=?new?RepeatBehavior(1);
????????????colorAnim.AutoReverse?=?false;
????????????Storyboard.SetTarget(colorAnim,?br);
????????????Storyboard.SetTargetProperty(colorAnim,?new?PropertyPath("Color"));
????????????storyboard.Children.Add(colorAnim);
????????????Resources.Add("colorsb",?storyboard);
????????????this.Loaded?+=?new?RoutedEventHandler(BackGorund_Loaded);
????????}
????} 創建ModelLocator ?? ?public?class?BackGroundModel?:?ModelLocator
????{
????????
????????private?static?readonly?BackGroundModel?_instance?=?new?BackGroundModel();
????????public?static?BackGroundModel?Instance?{?get?{?return?_instance;?}?}
????????static?BackGroundModel()
????????{
????????}
????????private?BackGroundModel()
????????????:?base()
????????{
????????}
????????private?byte?_R?=?(byte)0;
????????public?byte?R
????????{
????????????get?{?return?_R;?}
????????????set
????????????{
????????????????_R?=?value;
????????????????NotifyPropertyChanged("R");
????????????}
????????}
????????private?byte?_G?=?(byte)0;
????????public?byte?G
????????{
????????????get?{?return?_G;?}
????????????set
????????????{
????????????????_G?=?value;
????????????????NotifyPropertyChanged("G");
????????????}
????????}
????????private?byte?_B?=?(byte)0;
????????public?byte?B
????????{
????????????get?{?return?_B;?}
????????????set
????????????{
????????????????_B?=?value;
????????????????NotifyPropertyChanged("B");
????????????}
????????}
????}
控件Load時綁定屬性,通過模型來控制視圖
????????{
????????????this.DataContext?=?BackGroundModel.Instance;
????????????Binding?bindR?=?new?Binding("R");
????????????bindR.Mode?=?BindingMode.TwoWay;
????????????this.SetBinding(RProperty,?bindR);
????????????Binding?bindG?=?new?Binding("G");
????????????bindG.Mode?=?BindingMode.TwoWay;
????????????this.SetBinding(GProperty,?bindG);
????????????Binding?bindB?=?new?Binding("B");
????????????bindB.Mode?=?BindingMode.TwoWay;
????????????this.SetBinding(BProperty,?bindB);
????????}
?
提高效率
Shawn Wildermuth?寫了一個Code Snippets能幫我們快速的創建DependencyProperty屬性,具體用法與下載地址請訪問這里?。我自己寫了一個快速創建ModelLocator的Code Snippets,用法都是一樣,點擊這里?下載。 送上視頻 :) ViewManagerP1.wmv 基于Cairngorm的Silverlight開發 - part2轉載于:https://www.cnblogs.com/nasa/archive/2009/03/15/Cairngorm-Silverligh-part3.html
總結
以上是生活随笔為你收集整理的基于Cairngorm的Silverlight开发 - part3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmd查看mysql的ip地址_怎么在c
- 下一篇: PHP生成缩略图函数