gpt4 book ai didi

c# - WinForms 中的 MVP 导航

转载 作者:行者123 更新时间:2023-11-30 14:35:04 25 4
gpt4 key购买 nike

一直在学习 MVP,并尝试在 WinForms 中使用它编写测试应用程序。我正在努力寻找一个关于如何在我的表单/ View 之间导航的很好解释的示例。例如,程序启动,我想显示一个登录对话框,如果登录成功则进入我的主视图。目前,我的 Main 方法看起来像这样:

static void Main()
{
var loginView = Injector.Resolve<ILoginView>();
if (loginView.DoLogin() != LoginResult.OK) return;
var mainView = Injector.Resolve<IMainView>();
Application.Run(mainView); // won't work as mainView isn't a form
}

Injector 对象只是 IoC 工具(当前为 StructureMap)的包装器。问题是,我读到过我不应该真的通过 Injector 手动创建实例,因为它们实际上应该通过构造函数注入(inject)来完成。

我在一定程度上做到了这一点,但在导航方面却没有做到。我想不出一种优雅的方式来表达我的观点,想知道这里是否有人可以对此有所启发?我已经阅读了一些关于应用程序 Controller 的内容,但没有找到一个示例来清楚地展示它。

最佳答案

关于导航问题:

I've managed to do this up to a point but not when it comes to navigation. I can't think of an elegant way of moving through my views and was wondering if anyone here might shed some light on this? I've read a little on application controllers but have not found an example to show it clearly.

下面是我使用过的结构的简化版本。请注意,调用 NavigateTo 方法时会自动调用设置和拆卸 Hook 。另外,+1 给@AlexBurtsev,因为他的回答暗示了这种非常相同的方法。

// Presenter can and should offer common services for the
// subclasses
abstract class Presenter
{

public void Display()
{
OnDisplay();
}

public void Dismiss()
{
OnDismiss();
}


protected virtual OnDisplay() // hook for subclass
{
}

protected virtual OnDismiss() // hook for subclass
{
}

private NavigationManager _navMgr;

internal NavigationMgr NavigationManager
{
get
{
return _navMgr;
}
set
{
_navMgr = value;
}

}

}

// NavigationManager is used to transition (or navigate)
// between views
class NavigationManager
{

Presenter _current;

// use this override if your Presenter are non-persistent (transient)
public void NavigateTo(Type nextPresenterType, object args)
{
Presenter nextPresenter = Activator.CreateInstance(nextPresenterType);
NavigateTo(nextPresenter);
}

// use this override if your Presenter are persistent (long-lived)
public void NavigateTo(Presenter nextPresenter, object args)
{
if (_current != null)
{
_current.Dismiss()
_current.NavigationMgr = null;
_current = null;
}

if (nextPresenter != null)
{
_current = nextPresenter;
_current.NavigationMgr = this;
_current.Display(args);
}
}

}


class MainMenuPresenter : Presenter
{

private IMainMenuView _mainMenuView = null;

// OnDisplay is your startup hook
protected override void OnDisplay()
{
// get your view from where ever (injection, etc)
_mainMenuView = GetView();

// configure your view
_mainMenuView.Title = GetMainTitleInCurrentLanguage();
// etc
// etc

// listen for relevent events from the view
_mainMenuView.NewWorkOrderSelected += new EventHandler(MainMenuView_NewWorkOrderSelected);

// display to the user
_mainMenuView.Show();
}

protected override void OnDismiss()
{
// cleanup
_mainMenuView.NewWorkOrderSelected -= new EventHandler(MainMenuView_NewWorkOrderSelected);
_mainMenuView.Close();
_mainMenuView = null;
}

// respond to the various view events
private void MainMenuView_NewWorkOrderSelected(object src, EventArgs e)
{
// example of transitioning to a new view here...
NavigationMgr.NavigateTo(NewWorkOrderPresenter, null);
}

}


class NewWorkOrderPresenter : Presenter
{

protected override void OnDisplay()
{
// get the view, configure it, listen for its events, and show it
}

protected override void OnDismiss()
{
// unlisten for events and release the view
}

}

关于c# - WinForms 中的 MVP 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12956332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com