如何在设计器视图中正确加载资源?

时间:2021-07-10 20:12:43

标签: c# winforms visual-studio-2019 .net-4.7.2

我有一个包含资源文件的应用程序,假设它有一个背景图像和图标,我想在我的所有应用程序表单上使用它。在运行时,一切正常,表单加载背景图片和图标没有问题。但是,如果我在设计器中打开表单,我会得到:

<块引用>

“app.Properties.Resources”类型没有“my_icon”/“my_background”属性。

代码如下:

this.Icon = global::app.Properties.Resources.my_icon;
this.BackgroundImage = global::app.Properties.Resources.my_background;

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

根据我的测试,您可以将代码放在 Form1.Designer.cs-> InitializeComponent 方法中。

代码:

private void InitializeComponent()
        {
            // Load the resource file in designer view
            this.BackgroundImage = Properties.Resources.my_background;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.Icon = Properties.Resources.my_icon; 
            this.SuspendLayout();
            // 
            // Form1
            // 
     
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(731, 406);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

重建项目后,您可以在设计器视图中加载资源。

enter image description here

相关问题