我最近一直在研究 Android,并尝试将其功能之一移植到 C# 紧凑框架。
我所做的是创建一个抽象类,我称之为 Activity。这个类看起来像这样
internal abstract class Activity
{
protected Form myForm;
private static Activity myCurrentActivity = null;
private static Activity myNextActivity = null;
internal static void LoadNext(Activity nextActivity)
{
myNextActivity = nextActivity;
if (myNextActivity != null)
{
myNextActivity.Show();
if (myCurrentActivity != null)
{
myCurrentActivity.Close();
myCurrentActivity = null;
}
myCurrentActivity = myNextActivity;
myNextActivity = null;
}
}
internal void Show()
{
//PROBLEM IS HERE
Application.Run(myForm);
//myForm.Show();
//myForm.ShowDialog();
//
}
internal void Close()
{
myForm.Close();
}
internal void GenerateForm()
{
///Code that uses the Layout class to create a form, and then stores it in myForm
//then attaches click handlers on all the clickable controls in the form
//it is besides the point in this problem
}
protected abstract void Click(Control control);
//this receives all the click events from all the controls in the form
//it is besides the point in this problem
}
我遇到的问题是运行Show()
命令的一部分
基本上我所有的类都实现了上面的类,加载一个xml文件并显示出来。当我想转换到一个新的类/表单时(例如从 ACMain 到 ACLogIn)我使用这个代码
Activity.LoadNext(new ACLogIn);
应该加载下一个表单,显示它,然后卸载当前表单
我已经尝试过这些解决方案(在Show()
方法中),这是每个解决方案的问题
using
myForm.ShowDialog()
This works, but blocks execution, 这意味着旧表单不会关闭,并且我在表单之间移动的越多,进程堆栈增加的越多using
myForm.Show()
This works,在显示旧表单后关闭旧表单,但在此之后立即关闭程序并终止它使用
Application.Run(myForm)
这仅适用于加载的第一个表单,当我移动到下一个表单时,它会显示它然后抛出一个异常,说“值不在预期范围内”
有人可以帮我解决这个问题或找到替代方案吗?