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

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

问题描述

有吨StackOverflow上的问题,询问如何隐藏Form1并显示窗体2。和,通常,几个不同的答案冒出

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< WelcomeDialog方式>()

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.

编辑:

这是谁可能具有相同的麻烦别人一个pretty通用的问题(这样的要求是pretty打开)。具体到我的情况,虽然,我并不需要在启动时显示多种形式。另外,我有没有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.

我不认为一个Singleton模式将与形式的管理吻合。通常情况下,你要一些背景参数传递到表单,你可能想打开同一窗体的多个实例。因此,一个单身不合身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
        }
    }
}

当然,你可以写一些接口/仿制药的语法来避免的情况下,一点点code复制要显示大量的模态形式:

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;
    }
}

不过说实话,我觉得这是一个有点overingeneered。

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

第二点:如果你的形式不完全遵循这一特定行为(的ShowDialog()结果属性被设置),那么你必须写另​​一个接口...等。

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.

如果这种类型的语法(泛型,接口...等)不减少或书面code线的数量和复杂性的可维护性(显然,我们不能说这是真的如此这里),那么它的pretty没用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个可能的模态形式),我不会写复杂的code来管理这些表单。

  • 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天全站免登陆