gpt4 book ai didi

c# - 表单设计器打破了通用抽象 UserControl

转载 作者:太空狗 更新时间:2023-10-29 20:15:00 25 4
gpt4 key购买 nike

我有一个通用的抽象 UserControl 类,SensorControl,我希望我的所有传感器控制面板都继承自该类。

问题

当尝试设计 EthernetSensorControl(我从 Visual Studio 中继承的 UserControl 表单之一时,表单设计器中显示以下错误:

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: DeviceSensorControl --- The base class 'Engine.Sensors.SensorControl' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

传感器控制类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Engine.Sensors
{
public abstract class SensorControl<SensorType>
: UserControl where SensorType : class
{
protected SensorType _sensor;
public SensorControl(SensorType sensor)
{
_sensor = sensor;
}
}
}

示例继承类,EthernetSensorControl:

namespace Engine.Sensors
{
public partial class EthernetSensorControl
: SensorControl<EthernetSensor>
{
public EthernetSensorControl(EthernetSensor sensor)
: base(sensor)
{
}
}
}

调用堆栈:

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)

一切都可以编译,我可以看到显示的面板,但我无法设计它。我认为问题可能与 partial 类有关。有什么想法吗?

最佳答案

您不能设计继承抽象类的控件或窗体。

(设计者需要实例化基类作为设计界面)

基类还需要有一个无参构造函数供设计者调用。
此构造函数可以是私有(private)的。

关于c# - 表单设计器打破了通用抽象 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3933218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com