c# - 如何在 *.Designer.cs C# 中替换标签文本的硬编码字符串?

标签 c# visual-studio hardcode

如何正确替换 Form1.Designers.cs 中标签的硬编码字符串?

代替:

        this.button1.Text = "TheButton";
        this.label1.Text = "TheLabel";

我想写:

        this.button1.Text = Resources.ButtonText;
        this.label1.Text = Resources.LabelText;

在可视化设计器中更改表单后(添加任何组件并保存)VS 代码生成器将标签文本转回硬编码字符串:

        this.button1.Text = Resources.ButtonText;
        this.label1.Text = "TheLabel";

有人知道如何解决这个问题吗?

最佳答案

Prevent Auto code change in Designer.cs for a specific line

using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
     {
       public Form1()
         {
           InitializeComponent();
           this.button1.Text = Resources.ButtonText;
         }
     }
}

关于c# - 如何在 *.Designer.cs C# 中替换标签文本的硬编码字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064737/

相关文章:

json - Snowflake-如何在不使用硬编码键的情况下搜索 json 值中的值

c# - 如何取消 Hook C# 中的事件处理程序

C#编译器(csc.exe)内存溢出编译嵌套类型和Linq

c# - 从 'Copy always' 切换到 'Copy if newer' 的风险

git - Visual Studio 项目文件夹约定

python - 从 txt 文件读取到 3 个不同列表时如何防止硬编码 - python

javascript - 硬编码的 URL

C# - 压缩字节[]

c# - 在正确位置单击按钮时如何显示上下文菜单条

c# - 显示包含 '\t' 的字符串