在C#cf中显示在下一个表单之后关闭表单

Cru*_*ces 6 c# compact-framework winforms

我最近一直在研究Android,我试图将其中一个函数移植到C#compact framework.

我所做的是创建一个我称之为Activity的Abstract类.这个类看起来像这样

  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

   }
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是运行Show()命令的一部分

基本上我的所有类都实现上面的类,加载一个xml文件并显示它.当我想转换到新的类/表单(例如从ACMain转到ACLogIn)时,我使用此代码

Activity.LoadNext(new ACLogIn);
Run Code Online (Sandbox Code Playgroud)

应该加载下一个表单,显示它并卸载当前表单

我尝试了这些解决方案(在Show()方法中),这是每个解决方案的问题

  1. 使用myForm.ShowDialog()
    这可以工作,但阻止执行,这意味着旧表单不会关闭,我在表单之间移动的越多,进程堆栈增加的越多

  2. 使用myForm.Show()
    此工作,在显示旧表单后关闭旧表单,但在此之后立即关闭程序并终止它

  3. 使用Application.Run(myForm)
    这只适用于加载的第一个表单,当我移动到下一个表单时,它会显示它然后抛出一个异常,说"值不在预期的范围内"

有人可以帮我解决这个问题或找到替代方案吗?

cta*_*cke 5

如果你真的在为这个导航创建自己的框架之后,你需要重新思考.传入的Form实例Application.Run必须永远不会关闭 - 当它执行时,Application.Run完成执行并且(通常)您的static void Main入口点退出并且应用程序终止.

我建议你将Activity改为UserControl:

public abstract class Activity : UserControl
{
  .... 
}
Run Code Online (Sandbox Code Playgroud)

或撰写一个

public abstract class Activity
{
    private UserControl m_control;
  .... 
}
Run Code Online (Sandbox Code Playgroud)

然后,不是关闭和显示表单,而是将主Form中的所有活动作为容器.

作为公平的警告,当你开始想要在Tab主题而不是Stack中显示内容或者具有拆分视图时,这将变得复杂.框架似乎很容易创建,但它们并非如此,我至少考虑使用已经完成的东西,除非你有令人信服的理由想要自己推出.