Smart Client Software Factory View之间如何通信
2015-04-17 11:05
423 查看
有了前两篇关于事件如何发布,订阅,现在就有了做View之间通信的基础。
下面做一个小例子,点左边View中的按钮,随机改变右边的View的背景色。
如下图,我们在shell 工程下添加两个View,一个为LeftView,一个为RightView,添加时选择建立单独的文件夹。
这个Shell里面有两个Workspace,一个LeftWorkspace,一个RightWorkspace,我现在把LeftView显示到LeftWorkspace,RightView显示到RightWorkspace
题外话:
这个把View加入到Workspace的任务是由ModuleController来完成的,这个类可以自己写,但必须继承WorkItemController 类,也可以直接修改框架自动生成的ModuleController类。但不管怎样也好,调用的时候一定要调对应的ModuleController(不管是你自己创建的,还是框架自动生成的)
因为最终调用的地方是 Infrastructure.Module 工程下的 Module 类 ,其中调用的代码如下
[csharp] view
plaincopyprint?
public override void Load()
{
base.Load();
ControlledWorkItem<ModuleController> workItem = _rootWorkItem.WorkItems.AddNew<ControlledWorkItem<ModuleController>>();
workItem.Controller.Run();
}
其中的ModuleController可以是框架自动生成的,也可以是你自己写的(但要注意带上正确的命名空间)
回到主线:
刚刚上面已经添加了两个View,现在把它们加到Workspace中去
然后需要修改Infrastructure.Module 工程中的 ModuleController 类 ,由于LeftView和RightView 并不在 Infrastructure.Module 工程中,而是在Shell 中,那么需要在 Infrastructure.Module 中对Shell 工程进行引用 ,这样 ModuleController 中才可以操作 LeftView和RightView
引用完毕之后,我们修改 nfrastructure.Module 工程中的 ModuleController 类
[csharp] view
plaincopyprint?
private void AddViews()
{
//TODO: create the Module views, add them to the WorkItem and show them in
// a Workspace.
var leftView = ShowViewInWorkspace<LeftView>(WorkspaceNames.LeftWorkspace);
var rightView = ShowViewInWorkspace<RightView>(WorkspaceNames.RightWorkspace);
}
这样这两个View就加入到Workspace中了。
然后,接下来,我们要定义事件名称。在 Infrastructure.Interface 工程中的Constants文件夹下,找到EventTopicNames.cs 加入以下代码
[csharp] view
plaincopyprint?
public const string ChangeColor = "ChangeColor";
下一步,我们给LeftView 发布一个事件
右键 LeftViewPresenter.cs 按照前两篇文章的方法加入要发布的事件 ChangeColor
[csharp] view
plaincopyprint?
[EventPublication(EventTopicNames.ChangeColor, PublicationScope.Global)]
public event EventHandler<EventArgs> ChangeColor;
再定义了一个方法,这个方法随机产生一个颜色,并触发 ChangeColor 事件
[csharp] view
plaincopyprint?
public void ChangeViewColor()
{
Random random = new Random();
int r = random.Next(256);
int g = random.Next(256);
int b = random.Next(256);
System.Drawing.Color color = System.Drawing.Color.FromArgb(r, g, b);
if (ChangeColor != null)
{
ChangeColor(this, new EventArgs<System.Drawing.Color>(color));
}
}
之后我们在LeftView中放上一个按钮,起名叫changeColorButton ,设置Text 为 Change Color
双击按钮,修改按钮的事件代码(LeftView.cs中)
[csharp] view
plaincopyprint?
private void changeColorButton_Click(object sender, EventArgs e)
{
_presenter.ChangeViewColor();
}
这里调用了之前在Preserter中定义的ChangeViewColor 方法(方法中又触发了ChangeColor 事件 )。(View的按钮事件中并没有处理任何业务逻辑,所有的业务逻辑都应该放在Presenter中)
接下来的工作就是要修改RightView文件夹下的类了,
所先我们修改IRightView.cs 文件(在Presenter中,不直接调用View中的方法,而是调用IView中的)
[csharp] view
plaincopyprint?
namespace ViewComunicate.Infrastructure.Shell
{
public interface IRightView
{
void ChangeColor(System.Drawing.Color color);
}
}
然后修改RightView.cs来实现这个接口
[csharp] view
plaincopyprint?
public void ChangeColor(System.Drawing.Color color)
{
this.BackColor = color;
}
最后我们要在Present 中订阅这个事件,然后在事件中调用ChangeColor这个方法
修改RightViewPresenter.cs
[csharp] view
plaincopyprint?
[EventSubscription(EventTopicNames.ChangeColor, ThreadOption.UserInterface)]
public void OnChangeColor(object sender, EventArgs<System.Drawing.Color> e)
{
//TODO: Add your code here
View.ChangeColor(e.Data);
}
到这里,代码工作就已经完成了,下面是效果
下面做一个小例子,点左边View中的按钮,随机改变右边的View的背景色。
如下图,我们在shell 工程下添加两个View,一个为LeftView,一个为RightView,添加时选择建立单独的文件夹。
这个Shell里面有两个Workspace,一个LeftWorkspace,一个RightWorkspace,我现在把LeftView显示到LeftWorkspace,RightView显示到RightWorkspace
题外话:
这个把View加入到Workspace的任务是由ModuleController来完成的,这个类可以自己写,但必须继承WorkItemController 类,也可以直接修改框架自动生成的ModuleController类。但不管怎样也好,调用的时候一定要调对应的ModuleController(不管是你自己创建的,还是框架自动生成的)
因为最终调用的地方是 Infrastructure.Module 工程下的 Module 类 ,其中调用的代码如下
[csharp] view
plaincopyprint?
public override void Load()
{
base.Load();
ControlledWorkItem<ModuleController> workItem = _rootWorkItem.WorkItems.AddNew<ControlledWorkItem<ModuleController>>();
workItem.Controller.Run();
}
其中的ModuleController可以是框架自动生成的,也可以是你自己写的(但要注意带上正确的命名空间)
回到主线:
刚刚上面已经添加了两个View,现在把它们加到Workspace中去
然后需要修改Infrastructure.Module 工程中的 ModuleController 类 ,由于LeftView和RightView 并不在 Infrastructure.Module 工程中,而是在Shell 中,那么需要在 Infrastructure.Module 中对Shell 工程进行引用 ,这样 ModuleController 中才可以操作 LeftView和RightView
引用完毕之后,我们修改 nfrastructure.Module 工程中的 ModuleController 类
[csharp] view
plaincopyprint?
private void AddViews()
{
//TODO: create the Module views, add them to the WorkItem and show them in
// a Workspace.
var leftView = ShowViewInWorkspace<LeftView>(WorkspaceNames.LeftWorkspace);
var rightView = ShowViewInWorkspace<RightView>(WorkspaceNames.RightWorkspace);
}
这样这两个View就加入到Workspace中了。
然后,接下来,我们要定义事件名称。在 Infrastructure.Interface 工程中的Constants文件夹下,找到EventTopicNames.cs 加入以下代码
[csharp] view
plaincopyprint?
public const string ChangeColor = "ChangeColor";
下一步,我们给LeftView 发布一个事件
右键 LeftViewPresenter.cs 按照前两篇文章的方法加入要发布的事件 ChangeColor
[csharp] view
plaincopyprint?
[EventPublication(EventTopicNames.ChangeColor, PublicationScope.Global)]
public event EventHandler<EventArgs> ChangeColor;
再定义了一个方法,这个方法随机产生一个颜色,并触发 ChangeColor 事件
[csharp] view
plaincopyprint?
public void ChangeViewColor()
{
Random random = new Random();
int r = random.Next(256);
int g = random.Next(256);
int b = random.Next(256);
System.Drawing.Color color = System.Drawing.Color.FromArgb(r, g, b);
if (ChangeColor != null)
{
ChangeColor(this, new EventArgs<System.Drawing.Color>(color));
}
}
之后我们在LeftView中放上一个按钮,起名叫changeColorButton ,设置Text 为 Change Color
双击按钮,修改按钮的事件代码(LeftView.cs中)
[csharp] view
plaincopyprint?
private void changeColorButton_Click(object sender, EventArgs e)
{
_presenter.ChangeViewColor();
}
这里调用了之前在Preserter中定义的ChangeViewColor 方法(方法中又触发了ChangeColor 事件 )。(View的按钮事件中并没有处理任何业务逻辑,所有的业务逻辑都应该放在Presenter中)
接下来的工作就是要修改RightView文件夹下的类了,
所先我们修改IRightView.cs 文件(在Presenter中,不直接调用View中的方法,而是调用IView中的)
[csharp] view
plaincopyprint?
namespace ViewComunicate.Infrastructure.Shell
{
public interface IRightView
{
void ChangeColor(System.Drawing.Color color);
}
}
然后修改RightView.cs来实现这个接口
[csharp] view
plaincopyprint?
public void ChangeColor(System.Drawing.Color color)
{
this.BackColor = color;
}
最后我们要在Present 中订阅这个事件,然后在事件中调用ChangeColor这个方法
修改RightViewPresenter.cs
[csharp] view
plaincopyprint?
[EventSubscription(EventTopicNames.ChangeColor, ThreadOption.UserInterface)]
public void OnChangeColor(object sender, EventArgs<System.Drawing.Color> e)
{
//TODO: Add your code here
View.ChangeColor(e.Data);
}
到这里,代码工作就已经完成了,下面是效果
相关文章推荐
- Smart Client Software Factory 如何发布事件
- Model-View-Presenter Using The Smart Client Software Factory (Introduction To CAB/SCSF Part 25)
- Smart Client Software Factory 如何发布事件
- Smart Client Software Factory 如何订阅事件
- Smart Client Software Factory 如何使用Action Catalog
- Smart Client Software Factory 如何使用Action Catalog
- Smart Client Software Factory 如何使用Command
- Smart Client Software Factory 如何使用Command
- Smart Client Software Factory:扩展工具栏,加入新视图(View)
- Smart Client Software Factory April 2008 Alpha Release!
- How-To: Smart Client Software Factory (SCSF) & Visual Studio Orcas Beta 2(转自Southworks)
- SmartClient Software factory中的Composite UI Application Block(Cab)技术了解(八):Service
- 初识Smart Client Software Factory
- SCSF 系列:Smart Client Software Factory 启动过程详解
- 【单页应用之通信机制】view之间应该如何通信
- Smart Client Software Factory:CAB
- Smart Client Software Factory 2010安装条件