【问题标题】:Image button on visual studio 2010Visual Studio 2010 上的图像按钮
【发布时间】:2019-03-02 01:18:33
【问题描述】:

我正在尝试从 Visual Studio 2010 的 [设计] 视图中创建一个带有图像但没有文本的按钮。我拖放按钮,然后单击图像属性,从我的资源文件中选择一个 png 图像,但该属性始终为空,并且按钮在 [Design] 视图或编译程序中都没有图像。

我尝试在 Form.Designer.cs 文件中设置图像:

    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(73, 11);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(39, 34);
    this.button1.TabIndex = 10;
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Image = Properties.Resources.close_project_img;

当我转到 [设计] 视图时,我得到了错误:

To prevent possible data loss before loading the designer, the following errors must be resolved: 

Projectitem unavailable.     

Instances of this error (1)  

1.   Show Call Stack  

at EnvDTE.ProjectItem.get_FileCount()
at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.GetFileNameForProjectItem(ProjectItem item)
at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.BuildType()
at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.GetObjectType()
at Microsoft.VisualStudio.Shell.Design.GlobalType.get_ObjectType()
at Microsoft.VisualStudio.Shell.Design.GlobalObject.GetHashCode()
at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GetGlobalObjects(Type baseType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetTypeFromGlobalObjects(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetRuntimeType(String typeName)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetType(String typeName)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetType(String typeName)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyReferenceExpression(IDesignerSerializationManager manager, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)  

Help with this error  

Could not find an associated help topic for this error.

我可以点击“忽略并继续”,[Design] 视图可以正常打开,但是该按钮不会在该视图上显示图像(它会在编译的程序中显示)。

我在这里做错了什么?

【问题讨论】:

    标签: c# visual-studio visual-studio-2010 button imagebutton


    【解决方案1】:

    您不应直接编辑 Form.Designer.cs,因为它会被设计器覆盖。当您打开设计器时,设计器实际上会运行 Form.designer.cs 文件中的代码。由于该代码实际上是由 vs2010 运行的,所以它的环境有点不同

    只是一个测试,尝试通过button_click事件方法设置图像属性,可能是由于某种原因找不到图像

    【讨论】:

    • 如果我在 button_click 事件上设置图像属性,图像只会在单击按钮时出现,不是吗?那是行不通的。
    • 那是正确的,我只是说您可以这样做以确保正确找到并加载资源。表单设计器和 init 方法有时会导致难以发现这样的错误。
    【解决方案2】:

    我关闭了项目,删除了 resources.resx 和 resource.designer.cs,重新创建了资源文件,现在它工作正常,不知道是什么问题。

    【讨论】:

    • 有时表单设计器会卡住,而重置会修复它。其他时候,您可能需要违背 aL3891 的建议并自己编辑设计器文件以使其不被卡住。从您的 VCS 获取日志以帮助您查看真正的更改有助于调试这些问题。
    • 关闭项目并打开它对我有用。至少 20 分钟,我一直试图找出问题所在。现在我觉得自己很愚蠢
    【解决方案3】:

    重新启动 Visual Studio .NET

    我也遇到了同样的问题。调用堆栈不会导致或显示任何有助于在设计器文件中跟踪问题的信息。我只是关闭了 Visual Studio 2017,然后重新打开了该项目。问题自行解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多