多表单应用程序显示和隐藏表单的最佳实践? [英] Best practices for multi-form applications to show and hide forms?

查看:25
本文介绍了多表单应用程序显示和隐藏表单的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StackOverflow 上有很多关于如何隐藏 Form1 和显示 Form2 的问题.而且,通常会出现一些不同的答案:

There are tons of questions on StackOverflow asking how to hide Form1 and show Form2. And, usually, a few different answers crop up:

1)

// Program.cs
Application.Run(new Form1());
// Form1.cs
Form2 form2 = new Form2();
form2.Show();
this.Hide();

2)

// Program.cs
Form1 form1 = new Form1();
Form2 form2 = new Form2();
form1.Show();
form2.Show();
Application.Run();

...等..

我不是在寻找像#1 这样简单的一次性解决方案.我正在寻找最佳表单管理实践.一个包含 5-8 个表单、频繁打开和关闭的应用程序 - 管理这些表单的最佳方法是什么?

I'm not looking for a simple disposable solution like #1. I'm looking for best form management practices. An application with 5-8 forms, opening and closing one another frequently - what's the best way to manage these forms?

我的想法是让每个表单都成为(懒惰的?)单例并将它们埋在某种类型的 FormsManager 类中(如解决方案 #2 但 ++).然后单个表单可能会调用类似 FormsManager.GetForm() 之类的东西.

My idea was to make each form a (lazy?) Singleton and bury them in a FormsManager class of some sort (like solution #2 but ++). And then individual forms might call something like FormsManager.GetForm<WelcomeDialog>().

但我想知道有更多经验的人使用了什么.同样,这些解决方案不应该是快速的.它们应该面向设计,可能是架构,以及长期解决方案.

But I was wondering what people with more experience used. Again, these solutions shouldn't be quick hacks. They should be design-oriented, maybe architectural, and long-term solutions.

对于任何可能遇到同样问题的人来说,这是一个非常通用的问题(因此要求非常开放).不过,针对我的情况,我不需要在启动时显示多个表单.另外,我没有 MDI 表格.我可能有一些模态形式,但它们大多是非模态的.

This is a pretty generic question (so the requirements are pretty open) for anybody who might have the same trouble. Specific to my situation though, I don't need multiple forms shown at startup. Also, I have no MDI forms. I may have a few modal forms, but they are mostly non-modal.

推荐答案

我在这里以一般的方式回答.

I'm answering in a general manner here.

我认为单例模式不太适合表单管理.通常,您希望向表单传递一些上下文参数,并且您可能希望打开同一表单的多个实例.所以单身人士不太适合 IMO.

I don't think a singleton pattern would fit well with form management. Generally, you want to pass some context parameter to the form, and you might want to open multiple instances of the same form. So a singleton doesn't fit well IMO.

我认为表单管理应该很简单.

I think form management should be simple.

例如,如果你想从另一个表单中显示一个模态表单,我会写一些非常简单的东西:

For instance, if you want to display a modal form from another form, I would write something really straightforward:

private void button1_Click(object sender, EventArgs e)
{
    using (ModalForm1 frm = new ModalForm1(myParam))
    {
        frm.ShowDialog();

        if (frm.MyResultProperty == ...)
        {
            // Do some job here
        }
    }
}

当然你可以写一些接口/泛型语法来避免一些代码重复,以防你想显示很多模态形式:

Of course you could write some interface/generics syntax to avoid a little code duplication in case you want to display a lot of modal forms:

public interface IFormResult<T>
{
    T Result { get; set; }
}

public class ModalForm1 : Form, IFormResult<string>
{
    public ModalForm1()
    {
        InitializeComponent();

        this.Result = "My result";
    }

    public string Result { get; set; }
}

private void button1_Click(object sender, EventArgs e)
{
    string res = ShowModalForm<ModalForm1, string>();
}

private static T2 ShowModalForm<T1, T2>()
    where T1 : Form, IFormResult<T2>, new()
{
    using (T1 form = new T1())
    {
        form.ShowDialog();

        return form.Result;
    }
}

但老实说,我觉得它有点过分了.

But honestly, I feel like it's a bit overingeneered.

第二点:如果您的表单没有完全遵循此特定行为(ShowDialog() 然后设置了 Result 属性),那么您必须编写另一个接口...等

Second point: if your form doesn't exactly follows this specific behavior (ShowDialog() then a Result property is set), then you must write another Interface...etc.

如果这种类型的语法(泛型、接口...等)没有减少编写的代码行数或复杂性或可维护性(显然我们不能说这里确实如此),那么它是非常无用的 IMO.

If this type of syntax (generics, interfaces...etc.) doesn't reduce the number of lines of code written OR the complexity OR the maintainability (and obviously we can't say it's really the case here), then it's pretty useless IMO.

表单管理实际上取决于您的用例.

Form management really depends on your use case.

  • 如果您说可以同时显示 20 个表单,那么您应该考虑一个 FormManager 概念(或者更好:考虑如何通过减少表单数量来改善用户体验)可能打开的表格)
  • 如果您有一些相对简单的东西(同时有 2-3 个非模态表单 + 3-4 个可能的模态表单),我不会编写复杂的代码来管理这些表单.
  • If you have say 20 forms that can be displayed at the same time, then you should think of a FormManager concept (or better: think about how to improve the user experience by reducing the number for possible opened forms)
  • If you have something relatively simple (2-3 modeless forms at the same time + 3-4 possible modal forms), I wouldn't write complex code to manage those forms.

一般来说,用于启动应用程序的窗体(即关闭时停止程序的窗体,也就是作为Application.Run()的参数的窗体)负责其他形式.您有一个主表单和多个子表单.如果你的情况真的不同,那么可能写一些更聪明的东西,但这取决于你的情况.对于表单管理的普遍问题,我认为没有人可以提供一个普遍的好答案.

Generally, the form that is used to start the application (i.e. the form that stops the program when closed, which is the form that is a parameter of Application.Run()) is responsible of other forms. You have one main form, and multiples child forms. If your case is really different, then there is probably something smarter to write, but it'll depend on your case. I don't think one can provide a general good answer to the general problematic of form management.

老实说,如果您想要真正可维护的东西,请尝试(尽可能多地)减少可以同时显示的表单数量.在大多数情况下,同时显示多个无模式表单并不能提供良好的用户体验,如果表单相互依赖,表单生命周期管理可能会出现问题.

Honestly, if you want something really maintainable, try to reduce (as much as possible) the number of forms that can be shown at the same time. Multiple displayed modeless forms at the same time doesn't offer a good user experience in most cases, and form lifetime management can be problematic if forms are dependent on each other.

这篇关于多表单应用程序显示和隐藏表单的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆