简体   繁体   English

根据Visual Studio 2013中的资源文件设置按钮文本

[英]Set the Button Text according to Resource File in Visual Studio 2013

I am an Amateur Visual C# Programmer. 我是一名业余Visual C#程序员。 I want to implement the concept of I18N in my project. 我想在我的项目中实现I18N的概念。 (Similar to strings.xml in Android) (类似于Android中的strings.xml

I have added 2 String Resources in my Resource File. 我在资源文件中添加了2个字符串资源。

And I have added 2 Buttons in my form. 我在表单中添加了2个按钮。 Now, I want the text of the Button to be the string value from the Resource file. 现在,我希望Button的文本成为Resource文件中的字符串值。

Please help. 请帮忙。

Adhish Adhish

If you are using windows form, you can use this procedure. 如果您使用的是Windows窗体,则可以使用此过程。
Open the properties menu of the form in the design. 在设计中打开表单的属性菜单。 Select the option "Language" and choose the language that you want. 选择选项“语言”,然后选择所需的语言。
Selecting a different language will create automatically a new file resource with the label for the language selected. 选择其他语言将自动创建带有所选语言标签的新文件资源。
The file .resx will the same name of the form plus the initial of the language used. 文件.resx将具有与表单相同的名称以及所用语言的缩写。

It really depends on your UI framework. 它确实取决于您的UI框架。 The following solution is for WinForms, but you can implement it (without MVVM) in WPF as well. 以下解决方案适用于WinForms,但您也可以在WPF中实现它(不使用MVVM)。

  1. Create a button in the designer. 在设计器中创建一个按钮。
  2. Register to the Load event of the form by double clicking it. 通过双击将其注册到窗体的Load事件。 (Right click on the form => properties => click the events button => search for the Load event) (右键单击表单=>属性=>单击事件按钮=>搜索Load事件)
  3. In the load event, initialize the text of the button with the string from the resources. 在load事件中,使用资源中的字符串初始化按钮的文本。

Your code should look something like this: 您的代码应如下所示:

private void Form1_Load(object sender, EventArgs e)
{
    button1.Text = MyResources.ButtonText;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM