基于Cairngorm的Silverlight开发 - part3

简介: 使用ModelLocator来管理视图 之前只是简单的介绍了一下ModelLocator的用法,在这里要把模型和视图结合起来,通过模型来来控制视图。在Silverlight中我们可以看到所有新建立的xaml都是继承自UserControl,所以在这里更新欢称视图为控件。

使用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时绑定属性,通过模型来控制视图

         void  BackGorund_Loaded( object  sender, RoutedEventArgs e)
        {
            
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
相关文章
|
C# 前端开发
silverlight,WPF动画终极攻略之阳光灿烂篇(Blend 4开发)
原文:silverlight,WPF动画终极攻略之阳光灿烂篇(Blend 4开发) 前面我们画了一只会飞动的小鸟,今天我们在目标是一个会发光的太阳。本章节的动画虽然简单,但是实现的效果可是一点也不打折。
1232 0
|
C#
silverlight,WPF动画终极攻略之会飞的小鸟篇(Blend 4开发)
原文:silverlight,WPF动画终极攻略之会飞的小鸟篇(Blend 4开发) 本教程基本涵盖了WPF和silverlight中的各种动画。先上张效果图。 声明下,这个做的不是让大家照搬的,只是让大家熟悉下动画效果,这个成品基本涵盖了sl里面所有的动画效果。
1471 0
|
C#
silverlight,WPF动画终极攻略之迟来的第三章 动画整合篇(Blend 4开发)
原文:silverlight,WPF动画终极攻略之迟来的第三章 动画整合篇(Blend 4开发) 有个问题想请教下大家,我仿了腾讯的SL版QQ,相似度95%以上。我想写成教程教大家怎么开发出来,会不会有版权什么问题的。
1275 0
|
前端开发 C#
silverlight,WPF动画终极攻略之白云飘,坐车去旅游篇(Blend 4开发)
原文:silverlight,WPF动画终极攻略之白云飘,坐车去旅游篇(Blend 4开发) 这章有点长,所以我分成了两章。这一章主要是准备工作,差不多算美工篇吧,这章基本不会介绍多少动画效果,主要讲的是blend中工具的使用,利用哪些工具做出哪些效果。
1328 0
|
前端开发 C#
silverlight,WPF动画终极攻略之番外 3D切换导航篇(Blend 4开发)
原文:silverlight,WPF动画终极攻略之番外 3D切换导航篇(Blend 4开发) 这篇介绍的是3D导航,点击图标,页面360°翻转的效果!有什么不足的欢迎大家指出来。 1.新建一个usercontrol,命名为menu. 2.按照下图设置一下属性。
1325 0
|
自然语言处理 网络协议 数据格式
|
数据库 测试技术 安全
使用Entity Framework和WCF Ria Services开发SilverLight之2:POCO
在上一篇中《使用Entity Framework和WCF Ria Services开发SilverLight之1:简单模型》我们提出这类简单模型的几个问题: 1:实体模型被紧耦合在EDM中,同时它不能项目(模块)使用。
1294 0

热门文章

最新文章