无法解析资源Item winform资源文件

时间:2012-11-02 10:12:36

标签: c# winforms localization resources multilingual

这是多语言的代码:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsMultiLanguage
{
    public partial class Form1 : Form
    {
        ResourceManager m_resourceManger;
        public Form1()
        {
            InitializeComponent();

            m_resourceManger = new ResourceManager("WindowsFormsMultiLanguage.Localization", Assembly.GetExecutingAssembly());
            // Init UICulture to CurrentCulture
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
            // Init Controls
            UpdateUIControls();
        }



        private void UpdateUIControls()
        {
            try
            {
                if (m_resourceManger != null)
                {

                    this.label1.Text = m_resourceManger.GetString("test1");
                    this.label2.Text = m_resourceManger.GetString("test2");
                }
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        private void OnLanguageChange(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            string culture = string.Empty;

            switch (radioButton.Text)
            {

                case "French - France (fr-FR)":
                    culture = "fr-FR";
                    break;

                case "U.S. English (en-US)":
                    culture = "en-US";
                    break;

            }

            // This is used for the language of the user interface
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
        }

    }


}

我收到错误消息,此时无法解析资源项test&,test2: this.label1.Text = m_resourceManger.GetString("test1"); this.label2.Text = m_resourceManger.GetString("test2");

我已经添加了2个用于英语的资源文件1和用于法语的第2个资源文件,我不知道这是什么错误..

![在此处输入图片说明] [1] ![在此处输入图像说明] [2]

1 个答案:

答案 0 :(得分:0)

资源Localization可能不在命名空间Localization中,因此"Localization.Localization"构造函数的参数ResourceManager是错误的。我只是在这里猜测,因为没有足够的信息来发现问题。

编辑:我下载了项目,将其降级为我使用的Visual Studio 2008(在.sln中将数字11到10改为.ssproj中的两个地方改为4.0到3.5),并删除了对组件的引用Microsoft.CSharp 。该项目已经建成并且我运行它并且没有错误。函数UpdateUIControls在开始时只调用了一次,但在我更改语言时却没有。我将UpdateUIControls的两个主要行复制到OnLanguageChange的末尾,之后我更改语言时文本也发生了变化。

所以,我没有看到任何问题。问题出在其他地方。