我正在从事的项目是一个基于移动.NET CF的应用程序。我必须在其中实现MVP模式。我现在在其中使用OpenNETCF.IoC库和服务。
我必须将Windows代码重构为SmartParts。
在实现导航场景时,我遇到了一个问题:
// Show Main menu
bodyWorkspace.Show(mainMenuView);
// Show First view based on user choice
bodyWorkspace.Show(firstView);
// In first view are some value(s) entered and these values should be passed to the second view
bodyWorkspace.Show(secondView); // How?在Windows窗体逻辑中,这是用变量实现的:
var secondForm = new SecondForm();
secondForm.MyFormParameter = myFormParameter;如何用MVP术语重新实现这一逻辑?
发布于 2013-09-23 13:52:25
这在很大程度上取决于您的架构,但这将是我的建议:
首先,ViewB不需要ViewA中的信息。它需要模型或演示者中的信息。ViewA和ViewB应该从同一个地方得到他们的信息。
例如,可以通过服务来实现这一点。这个看起来可能是这样的:
class ParameterService
{
public int MyParameter { get; set; }
}
class ViewA
{
void Foo()
{
// could also be done via injection - this is just a simple example
var svc = RootWorkItem.Services.Get<ParameterService>();
svc.MyParameter = 42;
}
}
class ViewB
{
void Bar()
{
// could also be done via injection - this is just a simple example
var svc = RootWorkItem.Services.Get<ParameterService>();
theParameter = svc.MyParameter;
}
}在您正在使用的IoC框架中也支持的事件聚合也可以工作,其中ViewA发布ViewB订阅的事件。这方面的一个例子可以在这里找到,但一般来说,您将使用EventPublication和EventSubscription属性(前者用于ViewA中的事件,后者用于ViewB中的方法)。
https://stackoverflow.com/questions/18958718
复制相似问题