I have an application controller with navigation workflow like so:
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 Class:
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;
}
}
}
}
and in my Program.cs, I want to do this:
OrderScreen orderScreen = new OrderScreen();
orderScreen.Show();
NavigationWorkflow workflow = new NavigationWorkflow(orderScreen);
ApplicationController.Register(workflow);
Application.Run();
But whenever my main form (OrderScreen
) closes, the main application continues to run. How can I register my closing event with the Application.Run()
? Do I have to create my own ApplicationContext
? Is there some way to automatically do this?