Prism学习(7)---Commands

上一章中,对Shell, Region, View有了一个初步的了解。我们可以通过这些类向指定的用户控件加载Shell中,但是目前,还无法实现模块之间的交互。接下来开始开始逐步探索这方面的应用。

 

还是继续上一章中的例程接着往下改。在Prism中,允许在UI中绑定Command对象来实现MVVM模式。步聚如下: 

1. 在InterfaceProject模块中的ITextServices中修改代码,如下:

 

1      public  interface ITextService
2     {
3          string GetText();
4          void SetText( string newText);
5 
6          event EventHandler TextChanged;
7     }

2.在ModuleAProject项目的Services文件夹下,修改TextService.cs中的代码,如下:

 

 1      public  class TextService:ITextService
 2     {
 3          public TextService()
 4         {
 5             text =  " Hello Silverlight! ";
 6         }
 7          public  string GetText()
 8         {
 9              return text;
10         }
11          public  void SetText( string newText)
12         {
13              this.text = newText;
14 
15              if (TextChanged !=  null)
16             {
17                 TextChanged( this, EventArgs.Empty);
18             }
19         }
20 
21          string text;
22          public  event EventHandler TextChanged;
23     }

3, 通过以上的修改,完成模块对外接口及其实现。接下来, 需要在UI上加一些操作,用来与模块进行交互。在ModuleAViewOne.xaml中绑定好后,在ModuleAViewOneViewModel中更改代码如下: 

 

 1          public ModuleAViewOneViewModel(ITextService textService)
 2         {
 3              this.textService = textService;
 4 
 5              this.textService.TextChanged += (s, e) => {
 6                  if (PropertyChanged !=  null)
 7                 {
 8                     PropertyChanged( thisnew PropertyChangedEventArgs( " Text "));
 9                 }
10             };
11         }
12          public  string Text
13         {
14              get {  return textService.GetText(); }
15         }
16          public ICommand OnUpdateText
17         {
18              get
19             {
20                  if (onUpdateText ==  null)
21                 {
22                     onUpdateText =  new DelegateCommand< string>(
23                         OnTextChanged);
24                 }
25                  return onUpdateText;
26             }
27         }
28 
29          private ICommand onUpdateText;
30         ITextService textService;
31 
32          void OnTextChanged( string newText)
33         {
34             textService.SetText(newText);
35         }
36 
37          public  event PropertyChangedEventHandler PropertyChanged;
38     }

 

程序执行通过。明天继续。 代码点击这里下载!

转载于:https://www.cnblogs.com/prolove2/archive/2012/04/06/2435271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值