更新已关闭的表单的语言

时间:2017-08-04 07:02:09

标签: c#

目前我正在开发一个简单的多语言应用程序。此应用程序有两种形式:

表格1:选择语言(英语和意大利语)

表格2:欢迎信息

Form1.cs的

    private void button1_Click(object sender, EventArgs e)
    {
                Assembly a = Assembly.Load("LanguageManagement");
                ResourceManager rm = new ResourceManager("LanguageManagement.Language.English", a);

                Class1 lang = new Class1();
                lang.Language(rm);
            }

            private void button2_Click(object sender, EventArgs e)
            {
                Assembly a = Assembly.Load("LanguageManagement");
                ResourceManager rm = new ResourceManager("LanguageManagement.Language.Italian", a);

                Class1 lang = new Class1();
                lang.Language(rm);
            }
    }

当用户按下按钮1(英语)时,使用英语资源管理器并调用1级。

class Class1
    {
        public void Language(ResourceManager rm)
        {
            Form1 form1 = (Form1)Application.OpenForms["Form1"];
            form1.label1.Text = rm.GetString("_Name");
            form1.label2.Text = rm.GetString("_Surname");
            form1.label3.Text = rm.GetString("_Age");
            form1.button4.Text = rm.GetString("_Enter");

            //Form2 form = new Form2();
            Form2 form = (Form2)Application.OpenForms["Form2"];

            form.label1.Text = rm.GetString("_Welcome");
        }
    }

如果用户在选择语言之前未打开Form2,则会显示以下错误:

LanguageManagement.exe中出现未处理的“System.NullReferenceException”类型异常 附加信息:未将对象引用设置为对象的实例。

如何更新form2的语言,即使它从未打开过?

谢谢:)

3 个答案:

答案 0 :(得分:0)

您应该使用保存static的{​​{1}}成员,并且可以从任何地方访问。当ResourceManager打开时,它只需查看Form2成员并自行更改语言。

static

class Class1 { public static ResourceManager Resources { get; private set; } public void Language(ResourceManager rm) { Resources = rm; Form1 form1 = (Form1)Application.OpenForms["Form1"]; form1.label1.Text = rm.GetString("_Name"); form1.label2.Text = rm.GetString("_Surname"); form1.label3.Text = rm.GetString("_Age"); form1.button4.Text = rm.GetString("_Enter"); //Form2 form = new Form2(); Form2 form = (Form2)Application.OpenForms["Form2"]; } }

Form2

答案 1 :(得分:0)

以下是Form1设计者:

enter image description here

当用户按下Enter按钮时,将显示Form2并隐藏Form1。 如果用户在英语按钮上单击并且从未按下Enter按钮,则会显示以下错误:

LanguageManagement.exe中出现未处理的“System.NullReferenceException”类型异常附加信息:对象引用未设置为对象的实例。

答案 2 :(得分:0)

因此您似乎希望使用Class1作为填充代理。您可以创建方法static(因为它不使用任何类变量)并将表单传递给它以进行填充:

public void Language(ResourceManager rm, Form1 form1, Form2 form2)
{
    // FORM 1
    form1.label1.Text = rm.GetString("_Name");
    form1.label2.Text = rm.GetString("_Surname");
    form1.label3.Text = rm.GetString("_Age");
    form1.button4.Text = rm.GetString("_Enter");

    // FORM 2
    form2.label1.Text = rm.GetString("_Welcome");
}

您应该在Form2中的类级别实例化Form1对象,并将两个变量都传递给方法:

Form2 form2 = new Form2();

private void button1_Click(object sender, EventArgs e)
{
    Assembly a = Assembly.Load("LanguageManagement");
    ResourceManager rm = new ResourceManager("LanguageManagement.Language.English", a);

    // here both forms will be filled
    Class1.Language(rm, this, form2);
}

private void button2_Click(object sender, EventArgs e)
{
    Assembly a = Assembly.Load("LanguageManagement");
    ResourceManager rm = new ResourceManager("LanguageManagement.Language.Italian", a);

    Class1.Language(rm, this, form2);
}

您可以简化代码,因为两个按钮都有很多共同之处:

private void FillWithLanguage(string language)
{
    Assembly a = Assembly.Load("LanguageManagement");
    ResourceManager rm = new ResourceManager(language, a);

    // here both forms will be filled
    Class1.Language(rm, this, form2);
}

现在您可以将按钮代码缩减为:

private void button1_Click(object sender, EventArgs e)
{
    FillWithLanguageResourceManager("LanguageManagement.Language.English");
}

private void button2_Click(object sender, EventArgs e)
{
    FillWithLanguageResourceManager("LanguageManagement.Language.Italian");    
}