Windows 窗体、设计器和单例 [英] Windows Forms, Designer, and Singleton

查看:26
本文介绍了Windows 窗体、设计器和单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Windows 窗体和用户控件,到目前为止,这只是一个令人头疼的问题.我无法将表单或控件设为静态,因为设计师不喜欢那样,当我在表单和控件上使用 Singleton 时,设计师仍然会向我抛出错误.

I'm trying to work with Windows Forms and User Controls and thus far it's been nothing but a headache. I can't make the form or the controls static because the designer doesn't like that and when I use Singleton on my form and controls, the designer still throws errors at me.

我的 FormMain:

My FormMain:

public partial class FormMain : Form
{
    private static FormMain inst;

    public static FormMain Instance
    {
        get
        {
            if (inst == null || inst.IsDisposed)
                inst = new FormMain();
            return inst;
        }
    }

    private FormMain()
    {
        inst = this;
        InitializeComponent();
    }

MainScreen.cs:

MainScreen.cs:

public partial class MainScreen : UserControl
{
    private static MainScreen inst;

    public static MainScreen Instance
    {
        get
        {
            if (inst == null || inst.IsDisposed)
                inst = new MainScreen();
            return inst;
        }
    }

    private MainScreen()
    {
        inst = this;
        InitializeComponent();
    }

如果 MainScreen 的构造函数是 public 程序运行,但是当我将它更改为私有时,我现在在 FormMain.Designer.cs 中收到错误消息,说'Adventurers_of_Wintercrest.UserControls.MainScreen.MainScreen()' 由于其原因而无法访问保护级别".它指向这一行:

If the constructor of MainScreen is public the program runs, but when I change it to private I now get an error in FormMain.Designer.cs saying "'Adventurers_of_Wintercrest.UserControls.MainScreen.MainScreen()' is inaccessible due to its protection level". It points to this line:

        this.controlMainScreen = new Adventurers_of_Wintercrest.UserControls.MainScreen();

我认为这是设计者默认制作的类的实例.我应该放弃设计师吗?或者有没有办法解决这个问题?或者是否有另一种方法可以在不使用 Singleton 的情况下访问类属性(因为我似乎无法将表单或控件设为静态)?任何帮助将不胜感激.

I think this is the instance of the class that the designer makes by default. Should I ditch the designer? Or is there a way around this? Or is there another way to make class properties accessible without using Singleton (since I can't seem to make the form or controls static)? Any help would be greatly appreciated.

推荐答案

如果要访问实例化表单的公共属性,则需要保留对每个表单的每个实例的引用.

You need to keep a reference to each instance of each form if you want to access the public properties of the instantiated form.

一种方法是为每种类型的表单创建一个带有静态变量的类:

One way is to have a class with a static variable for each type of form:

class FormReferenceHolder
{
    public static Form1 form1;
    public static Form2 form2;
}

这样您就可以在实例化表单时设置静态变量,然后您可以从程序中的任何位置访问该变量.您可以更进一步,并使用设置表单的属性(如果表单尚不存在):

This way you would set the static variable whenever you instantiate a form, and then you can access that variable from anywhere in the program. You can go one step further with this and use properties that set up the form if it doesn't already exist:

class FormReferenceHolder
{
    private static Form1 form1;
    public static Form1 Form1
    {
        get
        {
            if (form1 == null) form1 = new Form1(); 
            return form1 ;
        }
    }
}

...

static void Main()
{
    Application.Run(FormReferenceHolder.Form1 );
}

这篇关于Windows 窗体、设计器和单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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