自定义 WinForms 按钮不更改图像? [英] Custom WinForms button does not change image?

查看:30
本文介绍了自定义 WinForms 按钮不更改图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义按钮,称为 AcceptButton,继承自 System.Windows.Forms.Button

I created a custom Button, called AcceptButton, inheriting from System.Windows.Forms.Button

在构造函数中,我设置了一些属性,但最重要的是一个图像(一个绿色的复选标记),如下所示:

On the constructor I set a few properties, but most important, an image (A green checkmark), like this:

this.Image = Proyecto.Controls.Bases.Properties.Resources.ok_16;

当我使用 VS2013 表单设计器添加此控件时,在另一个引用我刚刚创建的 DLL 的项目中,图像显示正确.但是如果我进入我的控制,并在代码中更改图像,例如:

When I add this control using VS2013 form designer, in another project that references the DLL I just created, the image is displayed correctly. But if I go into my control, and change the image in code, for example, to:

this.Image = Proyecto.Controls.Bases.Properties.Resources.ok_32;

在使用此控件的项目中,图像不会发生变化(即使溶液已清洁并重新生成).我按照VS2013生成的代码,发现设计者加了这一行:

The image is not changed in the projects that use this control (even if the solution is cleaned and regenerated). I followed the code generated by VS2013 and I found that the designer adds this line:

this.botonAceptar1.Image = ((System.Drawing.Image)(resources.GetObject("botonAceptar1.Image")));

出于某种原因,此资源在 VS 生成的资源文件中进行了硬编码",但在我重新生成解决方案时并未更新.

For some reason, this resource is "hardcoded" in a resource file generated by VS, but it's not updated when I regenerate the solution.

删除此行使其按预期工作(我可以更改上游"类中的图像,并在重新生成解决方案时进行更新).

Removing this line makes it work as expected (I can change the image in the "upstream" class and it'll be updated when the solution is regenerated).

为什么会这样?我怎样才能避免这种情况?

Why is this happening? How can I avoid this?

推荐答案

这是由于 DesignerSerializationVisibility (MSDN) 属性.尝试添加此属性和这些方法 (MSDN) 到您的班级:

This happens due to the DesignerSerializationVisibility (MSDN) attribute. Try adding this property and these methods (MSDN) to your class:

public class MyButton : System.Windows.Forms.Button
{
    public bool ShouldSerializeImage()
    {
        return !object.ReferenceEquals(this.Image, _BaseImage);
    }

    public void ResetImage()
    {
        this.Image = _BaseImage;
    }

    [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)]
    public new Image Image
    {
        get { return base.Image; }
        set { base.Image = value; }
    }
    private Bitmap _BaseImage;
    public MyButton()
    {
        _BaseImage = Proyecto.Controls.Bases.Properties.Resources.ok_16;
        this.Image = _BaseImage;
    }
}

这会替换默认的 Image 属性并阻止您遇到的序列化.此外,它允许设计人员检查属性是否具有默认值以及是否需要序列化.默认值存储在按钮类的私有字段中.这应该正确序列化(或不序列化)属性.

This replaces the default Image property and prevents the serialization you encountered. Furthermore it allows the designer to check if the property has it's default value and if it needs to be serialized. The default value is stored in a private field in the button class. This should correctly serialize (or not serialize) the properties.

删除您拥有的所有按钮,重新编译,阅读按钮以确保.

Remove all buttons you have, recompile, readd the buttons to make sure.

这篇关于自定义 WinForms 按钮不更改图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆