我为Presenter
创建了抽象类public abstract class MvpPresenter<T>
{
public T View { get; set; }
public MvpPresenter()
{
}
public MvpPresenter(T view)
{
View = view;
}
}
和继承自抽象Presenter的UserPresenter类
public class UserPresenter2 : MvpPresenter<IUserView>
{
private void OnUserSave(object sender, EventArgs e)
{
if (View.ContextData.IsDirty)
{
User user = new User();
User domainUser = DataMapper.Translate(View.ContextData);
new UserServiceStub().SaveUser(domainUser);
}
}
private void OnUserSearch(object sender, SearchEventArgs e)
{
if (string.IsNullOrEmpty(e.SearchCriteria))
{
View.StatusMessage = "User name can not be null";
return;
}
User user = new UserServiceStub().GetUser(e.SearchCriteria);
if (user == null)
{
View.StatusMessage = String.Format(
"There's no user found for user name:{0}", e.SearchCriteria);
return;
}
UserDTO userDTO = DataMapper.Translate(user);
View.ContextData = userDTO;
}
}
视图代码:
public abstract class MvpView<TPresenter, TView> : Page
where TPresenter : MvpPresenter<TView>
{
public TPresenter Presenter { get; set; }
public MvpView()
: base()
{
if (!(this is TView))
throw new Exception("MvpView must implement the interface provider as generic TView type");
// Create and initialize presenter
Presenter = Activator.CreateInstance<TPresenter>();
Presenter.View = (TView)((object)this);
}
}
问题是当用户点击搜索时,页面没有响应。我知道搜索事件尚未在View中注册,但我无法在构造函数中执行。