【问题标题】:Windows Forms Designer Custom Control from Label has wrong property value -> designer exception标签中的 Windows 窗体设计器自定义控件具有错误的属性值 -> 设计器异常
【发布时间】:2011-02-08 11:52:50
【问题描述】:

我在这里实际上有点困惑,我已经构建了一个带有自定义控件的 dll,该控件基于 Label,AutoSize 固定为 true。我很高兴地使用它,但设计师创建的控件仍然设置为 AutoSize。

我更改了 dll 以试图让设计器显示我的控件,并将 AutoSize 设置为 false,并最终在设计器中抛出异常。

我撤消了最后一次更改,但仍然出现异常!

在我从头开始创建一个新项目并删除对 dll 的所有引用之前,我无法取得进展。

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

这是我认为发生的事情:

DLL 是在它自己的项目(和解决方案)中创建的,我已将其添加到工具箱中,并在不同项目的设计器中使用。

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

在表单上添加的标签没有将AutoSize 设置为 false,这正是我想要的。我只想让设计器视图反映运行时行为。

当我添加属性[DesignerSerializationVisibility(DesignerSerializationVisibility::Content)] 我在设计器中抛出异常

[我不确定这是否真的是问题,因为撤消它并没有解决我的问题]

输出

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) 

代码

#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;
        }
    }

// ... 

}

【问题讨论】:

    标签: .net c++ visual-studio-2010 custom-controls windows-forms-designer


    【解决方案1】:
        void set(bool x) override
        {
            this->AutoSize = false;
        }
    

    这是一个错误,你再次调用 setter。一旦您将控件放在表单上,​​这将使设计器崩溃并导致堆栈溢出。修复:

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

    您还必须将属性应用于属性,而不是 getter。

    【讨论】:

    • 太棒了!多么惊人的快速答案,甚至更好的是,它工作得很好。非常非常感谢。
    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 2011-05-25
    • 2013-01-23
    • 2010-09-27
    • 2010-11-23
    • 2010-09-08
    • 1970-01-01
    • 2010-10-28
    相关资源
    最近更新 更多