我有一个带有导航工作流程的应用程序控制器,如下所示:
namespace Ordering.UI.Workflow
{
public static class ApplicationController
{
private static INavigationWorkflow instance;
private static string navigationArgument;
public static void Register(INavigationWorkflow service)
{
if (service == null)
throw new ArgumentNullException();
instance = service;
}
public static void NavigateTo(string view)
{
if (instance == null)
throw new InvalidOperationException();
instance.NavigateTo(view, Argument);
}
public static void NavigateTo(string view, string argument)
{
if (instance == null)
throw new InvalidOperationException();
navigationArgument = argument;
NavigateTo(view);
}
public static string Argument
{
get
{
return navigationArgument;
}
}
}
}
NavigationWorkflow类:
namespace Ordering.UI.Workflow
{
public interface INavigationWorkflow
{
void NavigateTo(string uri, string argument);
}
public class NavigationWorkflow : INavigationWorkflow
{
Form _mainForm;
ProductScreen productScreen;
public NavigationWorkflow() { }
public NavigationWorkflow(Form mainForm)
{
_mainForm = mainForm;
}
public void NavigateTo(string view, string argument)
{
switch (view)
{
case "products":
if (productScreen != null && !productScreen.IsDisposed)
{
productScreen.Close();
productScreen = null;
}
if (productScreen == null && productScreen.IsDisposed)
{
productScreen = new ProductScreen();
}
productScreen.Show();
break;
}
}
}
}
在我的Program.cs中,我想这样做:
OrderScreen orderScreen = new OrderScreen();
orderScreen.Show();
NavigationWorkflow workflow = new NavigationWorkflow(orderScreen);
ApplicationController.Register(workflow);
Application.Run();
但只要我的主表单(OrderScreen
)关闭,主应用程序就会继续运行。如何使用Application.Run()
注册我的结束活动?我是否必须创建自己的ApplicationContext
?有没有办法自动执行此操作?
答案 0 :(得分:2)
Application.Run()自动创建ApplicationContext。您可以使用Application.ApplicationContext属性获取对它的引用。调用其ExitThread()方法强制消息循环终止。
创建自己的并将其传递给Run()也是可能的,没有我能想到的真正优势。除了作为控制器的基类之外。 Run()方法调用逻辑上也属于您的控制器。