Visual Studio 2010 上的图像按钮

发布于 2024-10-19 08:13:19 字数 3541 浏览 4 评论 0原文

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

我尝试使用以下方法在 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.

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

我在这里做错了什么?

I am trying to create a button with image and no text from the [Design] view on Visual Studio 2010. I drag & dropped the button, then I click the Image Property, I select a png image from my resource file, but the property always remain empty and the button doesn't have image neither on the [Design] view or in the compiled program.

I tried setting the image on the Form.Designer.cs file with:

    // 
    // 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;

And when I go to the [Design] View, I get the error :

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.

I can click "ignore and continue" and the [Design] view opens fine, but the button doesn't show the image on that view (it does show it in the compiled program).

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身骄傲 2024-10-26 08:13:19

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

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

you should not edit the Form.Designer.cs directly because it will be overwritten by the designer. the designer actually runs the code in the Form.designer.cs file when you open the designer. since that code is actually run by vs2010, its environment is a little diffrent

just a test, try setting the image property from the button_click event method instead, it might be that the image is not found for some reason

鱼忆七猫命九 2024-10-26 08:13:19

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

I closed the project, removed the resources.resx and resource.designer.cs, recreated the resources files and it works fine now, not sure what was the problem.

心的憧憬 2024-10-26 08:13:19

重新启动 Visual Studio .NET

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

Restart Visual Studio .NET

I had the same issue happen to me. The call stack doesn't lead to or show any information that is helpful to tracking down the issue in the designer file. I simply closed Visual Studio 2017 and then reopened the project. Problem fixed itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文