2013-09-23 80 views
1

我正在開發的項目是基於移動.NET CF的應用程序。我必須在其中實施MVP模式。我現在正在使用OpenNETCF.IoC庫和服務。將Windows窗體重構爲MVP

我必須將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而言,這什麼邏輯?

回答

1

這在很大程度上取決於你的架構,但是這將是我的建議:

首先,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; 
    } 
} 

事件彙總,還支持the IoC framework you're using,也可以工作,其中ViewA發佈該ViewB訂閱的事件。 An example of this can be found here,但一般來說,您將使用EventPublicationEventSubscription屬性(前者在ViewA中的事件,後者在ViewB中的方法中)。