我正在为我当前的项目(Windows 应用程序)学习 MVP 模式。 我在 Silverlight 和 WPF 中使用 MVVM 方面有很好的工作经验。 在 MVVM 中,我的 View 和 ViewModel 曾经位于单独的项目中,并使用 WPF 的强绑定(bind)来相互通信。 但是在 MVP 中,我在 Internet 上看到的大多数示例中,View 和 Presenter 在同一个项目中。
所以我的问题是:- 有什么方法可以在不同的项目中创建 View 和 Presenter?我的意思是将 View 作为 Windows 应用程序,将 Presenter 作为类库项目。
如果是,那么我的 View 和 Presenter 如何相互引用。
最佳答案
您的演示者应该只通过界面与 View 通信。
您的presenter 和view 接口(interface)可以包含在windows 应用程序可以引用的类库项目中。您在 Windows 应用程序项目中创建的任何具体 View 都可以实现适当的 View 界面。
下面的简单示例显示了类如何交互。
ClassLibrary.dll
public class Presenter {
// Repository class used to retrieve data
private IRepository<Item> repository = ...;
public void View { get; set; }
public void LoadData() {
// Retrieve your data from a repository or service
IEnumerable<Item> items = repository.find(...);
this.View.DisplayItems(items);
}
}
public interface IView {
void DisplayItems(IEnumerable<Item> items);
}
WindowsApplication.dll
public class ConcreteView : IView {
private Button btn
private Grid grid;
private Presenter presenter = new Presenter();
public ConcreteView() {
presenter.View = this;
btn.Click += (s, a) => presenter.LoadData();
}
public void DisplayItems(IEnumerable<Item> items) {
// enumerate the items and add them to your grid...
}
}
关于c# - 我可以在 MVP 模式的不同项目中创建 View 和 Presenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30930837/