【问题标题】:How resx, designer and cs file pass values within the form?resx、designer 和 cs 文件如何在表单中传递值?
【发布时间】:2020-08-26 06:55:06
【问题描述】:

Windows 形式。 form.resx中有xml数据

<data name="$this.Text" xml:space="preserve">
  <value>Report</value>
</data>

所以在 form.designer.cs 中

public System.Windows.Forms.ListView report;
private void InitializeComponent()
{
this.report.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.report_ColumnClick);
}

在form.cs中

private void report_ColumnClick(Object eventSender, ColumnClickEventArgs eventArgs)
        {
            if (this.Text != "Report")
            {
                //Some code
            }
        }

问题是 form.resx 中的值如何在 form.cs 中得到识别。 this.text 如何在 desginer 和 cs 文件中被识别

【问题讨论】:

  • 你没有显示完整的InitializeComponent(),资源由ComponentResourceManager加载。

标签: c# winforms windows-forms-designer resx


【解决方案1】:

Form.csForm.designer.cs 之间的关系由类名和partial 关键字决定。

你可以把类分成多个部分或文件,只要你给类起相同的名字,并在它前面加上partial关键字,编译时编译器会认为这是一个大类.. 例如。

Forms.cs 文件

partial class Form
{
 //contain all the implementation code for the form
 //all the code added by the programmer
}

Form.designer.cs 文件

partial class Form
{
// contains all the auto generated code
// contains the InitializeComponent() method
}

编译后,编译器会将上述两个文件都视为Form 的一类。

.resx 文件,see this answer

.resx 文件还有助于 Visual Studio 在设计时跟踪要在设计时显示的值。

如果您想更改代码中的this.Text,您可以通过Load 事件的形式进行。 例如。

private void Form1_Load(object sender, EventArgs e)
{
    string oldText = this.Text; //oldText will be 'Report' or 'Form1'
    this.Text = "whatever you want it to be";
}

【讨论】:

  • resx 的值会是 i18n 吗?
  • 谢谢。为了让我知道区别。但我真的很想知道如何在 .cs 中识别 .resx xml 值。
  • 它将为每种语言创建一个 .regex 文件。例如,如果您在表单上选择法语语言,则会创建另一个具有 i18n 设置的 .resx 文件 Form1.fr.resx
  • 据我所知,您不能像使用类一样在代码中使用它。我从来没有以这种方式使用它。我认为它的工作方式与可以使用Properties.Settings.Default.SomeSettingYouAdded 或资源Properties.Resources.SomeResourceYouAdded 访问的设置文件不同
  • 在三个地方使用了相同的表单,并且 this.Text 被覆盖。有什么方法可以将覆盖的文本与原始 this.text 进行比较?在这里,我硬编码为“报告”。所以我可以动态地做而不是这样做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
相关资源
最近更新 更多