来自Label的Windows窗体设计器自定义控件的属性值错误 - >设计器异常 [英] Windows Forms Designer Custom Control from Label has wrong property value -> designer exception

查看:392
本文介绍了来自Label的Windows窗体设计器自定义控件的属性值错误 - >设计器异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我其实有点困惑这里,我已经建立了一个dll与基于Label的自定义控件AutoSize固定为true。我很高兴地使用它,但设计器创建控件与AutoSize仍然设置为真。



我改变了dll,试图让设计器显示我的控制与AutoSize设置false,并在设计器中抛出一个异常。



我忽略了最后一次更改,但仍有异常!



我无法进步,直到我从头开始创建一个新项目,删除所有引用的dll。



我真的不知道那里发生了什么。 / p>

这是我想到的是发生的:



dll是在自己的项目



它包含基于 System :: Windows的自定义控件:: Forms :: Label AutoSize 总是false。 (我知道这很简单,但是当我知道我在做什么时,我打算扩展它。)



表单上添加的标签没有 AutoSize 设置为false这是我想要的。



当我添加属性 [DesignerSerializationVisibility(DesignerSerializationVisibility :: Content)]

当设置器视图反映运行时行为时, code>
我在设计器中抛出异常



[我不知道这是否真的是因为撤消它的问题整理我]



输出



 在VSLangProj.Reference.get_Path ()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_FileName()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.GetMatchIndex(String typeName)
at Microsoft.VisualStudio.Design .VSTypeResolutionService.SearchNormalEntries(AssemblyName assemblyName,String typeName,Boolean ignoreTypeCase,Assembly& assembly,Boolean fastSearch)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchEntries(AssemblyName assemblyName,String typeName,Boolean ignoreCase,Assembly& assembly,ReferenceType refType)
在$ Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name)时,
在Microsoft.VisualStudio.Design.VSTypeResolutionService.GetType(String typeName,Boolean throwOnError,Boolean ignoreCase,ReferenceType refType) ,Boolean throwOnError,Boolean ignoreCase)
在Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name)
在System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost。在System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager经理)
在System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager经理)
$ Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
在System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoader主机)



代码



  #pragma once 

#using< System.DLL>
#using< System.Drawing.DLL>
#using< System.Windows.Forms.DLL>

命名空间EasyButtons {

使用命名空间System;
using namespace System :: ComponentModel;
using namespace System :: Collections;
using namespace System :: Windows :: Forms;
using namespace System :: Data;
using namespace System :: Drawing;


public ref class ResizeLabel:public System :: Windows :: Forms :: Label
{
public:ResizeLabel(void){
this- > AutoSize = false;
}
public:
virtual property bool AutoSize
{
[DesignerSerializationVisibility(DesignerSerializationVisibility :: Content)]
bool get()override
{
return false;
}

void set(bool x)override
{
this-> AutoSize = false;
}
}

// ...

}


解决方案

  void set(bool x)override 
{
this-> AutoSize = false;
}

这是一个错误,你再次调用setter。这将使设计器崩溃的堆栈溢出,只要你把控件放在窗体上。修正:

  void set(bool x)override 
{
__super :: AutoSize = false;
}

您还必须在属性上应用属性, p>

I'm actually a bit confused here, I've built a dll with a custom control based on Label with AutoSize fixed as true. I happily used it but the designer created the control with AutoSize still set to true.

I changed the dll in an attempt to get the designer to show my control with AutoSize set false and ended up with a thrown exception in the designer.

I undid the last change but still got the exception!

I couldn't progress until I created a new project from scratch removing all reference to the dll.

I really don't know what went on there.

Here's what I thought was happening:

The dll is created in it's own project (& solution too) which I've added to the toolbox and used in the designer in a different project.

It contains a custom control based on System::Windows::Forms::Label with AutoSize always false. (I know this is very simple, but I intend to extend it when I know what I'm doing!)

The label added on the form does not have AutoSize set to false which is what I want. I just want the designer view to reflect the run time behaviour.

When I added the attribute [DesignerSerializationVisibility(DesignerSerializationVisibility::Content)] I get an exception thrown in the designer

[I'm not sure if this is really the problem as undoing it didn't sort me out]

Output

at VSLangProj.Reference.get_Path()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_FileName()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.GetMatchIndex(String typeName)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchNormalEntries(AssemblyName assemblyName, String typeName, Boolean ignoreTypeCase, Assembly& assembly, Boolean fastSearch)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchEntries(AssemblyName assemblyName, String typeName, Boolean ignoreCase, Assembly& assembly, ReferenceType refType)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, ReferenceType refType)
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.DesignerHost.System.ComponentModel.Design.IDesignerHost.GetType(String typeName)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) 

Code

#pragma once

#using <System.DLL>
#using <System.Drawing.DLL>
#using <System.Windows.Forms.DLL>

namespace EasyButtons {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


public ref class ResizeLabel : public System::Windows::Forms::Label
{
public: ResizeLabel(void) {
        this->AutoSize = false;
    }
public:
    virtual property bool AutoSize
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility::Content)]
        bool get() override
        {
            return false;
        }

        void set(bool x) override
        {
            this->AutoSize = false;
        }
    }

// ... 

}

解决方案

    void set(bool x) override
    {
        this->AutoSize = false;
    }

That's a bug, you call the setter again. This will crash the designer with a stack overflow as soon as you put the control on a form. Fix:

    void set(bool x) override
    {
        __super::AutoSize = false;
    }

You must also apply the attribute on the property, not the getter.

这篇关于来自Label的Windows窗体设计器自定义控件的属性值错误 - &gt;设计器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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