博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Prism学习(7)---Commands
阅读量:4334 次
发布时间:2019-06-07

本文共 1712 字,大约阅读时间需要 5 分钟。

上一章中,对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(
this
new 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

你可能感兴趣的文章
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------&gt;Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>