【发布时间】:2012-10-14 12:16:56
【问题描述】:
我最近一直在研究 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() 方法中),这是每个解决方案的问题
使用
myForm.ShowDialog()
这可行,但会阻止执行,这意味着旧表单不会关闭,并且我在表单之间移动的次数越多,进程堆栈增加的越多使用
myForm.Show()
这可行,在显示旧表单后关闭旧表单,但之后立即关闭程序并终止它使用
Application.Run(myForm)
这仅适用于加载的第一个表单,当我移动到下一个表单时,它会显示它然后引发异常说“值不在预期范围内”
有人可以帮我解决这个问题或找到替代方法吗?
【问题讨论】:
-
主要问题是您只能拥有 1 个主表单。查看 Aplication 是否具有可写 MainForm 属性,然后将其与 Show() 一起使用。
标签: c# winforms compact-framework