我在Windows窗体中遇到了继承控件的问题,需要一些建议.
我确实为List中的项目(由面板构成的自制GUI列表)和一些可以添加到列表中的每种数据类型的继承控件使用基类.
它没有问题,但我现在发现,将基本控件变成抽象类是正确的,因为它有方法,需要在所有继承的控件中实现,从内部的代码调用base-control,但不能也不能在基类中实现.
当我将基本控件标记为抽象时,Visual Studio 2008 Designer拒绝加载窗口.
有没有办法让Designer使用基础控件制作抽象?
我为WinForm UserControl创建了一个通用基类:
public partial class BaseUserControl<T> : UserControl
{
public virtual void MyMethod<T>()
{
// some base stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
以及基于以下内容的UserControl:
public partial class MyControl : BaseUserControl<SomeClass>
{
public override void MyMethod<SomeClass>()
{
// some specific stuff here
base.MyMethod<SomeClass>();
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但MyControl无法在VisualStudio Designer中进行编辑,因为它表示无法加载基类.我试图定义另一个类BaseUserControl,非泛型,希望它会加载它,但这个技巧似乎不起作用.
我已经有一个解决方法:定义一个接口,IMyInterface <T>,然后创建我的控件
public partial class MyControl : UserControl, IMyInterface<SomeClass>
Run Code Online (Sandbox Code Playgroud)
但是我失去了我的基本虚拟方法(不是很重要,但仍然......).
有没有办法为UserControl创建基础泛型类,有可能在VisualStudio Designer中编辑它?
在我的UI XAML中,我基本上继承了一个类"BaseView",它包含多个表单共有的功能,但是这阻止了设计者显示表单:"无法创建BaseView类型的实例".代码将编译并运行,但是不能在Designer中看到该表单是令人沮丧的.有没有更好的办法?谢谢.
XAML:
<vw:BaseView
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:ReviewClient"
x:Class="ReviewClient.MainPage"
Run Code Online (Sandbox Code Playgroud)
...
我有一个通用的抽象UserControl类,SensorControl我希望我的所有传感器控制面板都可以继承.
尝试EthernetSensorControl在Visual Studio中设计(我继承的UserControl表单之一)时,表单设计器中显示以下错误:
无法为此文件显示设计器,因为其中的所有类都无法设计.设计人员检查了文件中的以下类:DeviceSensorControl ---无法加载基类"Engine.Sensors.SensorControl".确保已引用程序集并且已构建所有项目.
SensorControl 类: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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
EthernetSensorControl:namespace Engine.Sensors
{
public partial class EthernetSensorControl
: SensorControl<EthernetSensor>
{
public EthernetSensorControl(EthernetSensor sensor)
: base(sensor)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)的System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)的System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)中在System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost主机)
一切都编译好,我可以看到显示的面板,但我无法设计它.我认为问题可能与partial …
c# inheritance templates partial-classes windows-forms-designer
我正在关注winforms的这个教程,到目前为止,教程正在编写表单而不使用工具箱.我相信它很快就会更深入地介绍工具箱.
在本教程之后,我在以下两段代码中进行了部分类:
第一档:
using System;
using System.Windows.Forms;
public class Numeric : System.Windows.Forms.TextBox
{
public Numeric()
{
}
}
public partial class Exercise
{
private Numeric txtbox;
System.ComponentModel.Container components;
}
Run Code Online (Sandbox Code Playgroud)
第二档:
using System;
using System.Windows.Forms;
public partial class Exercise : Form
{
private void InitializeComponent()
{
txtbox = new Numeric();
Controls.Add(txtbox);
}
public Exercise()
{
InitializeComponent();
}
}
public class program
{
public static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用F5运行代码时,一切看起来都很好:表单弹出文本框.
但出于某种原因,当我右键单击第二个文件并选择"视图设计器"时,我收到一条错误,上面写着"变量'txtbox'未声明或未被分配".我可以选择"忽略并继续",这会将我引导到一个没有文本框的表单.
为什么会这样?我知道有些人认为我应该使用工具箱,这可能是最明智的做法,但我仍然想知道为什么会这样.
在我的一个项目中,我使用了一个抽象的 UserControl。为了能够在 Visual Studio 中设计此控件,我使用了此答案中提出的代码。现在我想将其与另一个通用的抽象 UserControl 一起使用。但如果我这样做
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<MyBaseControl<T>, UserControl>))]
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误
CS0416:属性参数不能使用类型参数
删除类型参数显然也无法编译。
我无法从非泛型基类派生 MyBaseControl,因为它已经从泛型基类派生,因此我尝试用接口装饰它并像这样使用它:
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<IMyBaseControl, UserControl>))]
Run Code Online (Sandbox Code Playgroud)
这确实可以编译,但是当我打开设计视图时,我的控件没有被渲染,而是出现错误
提供的泛型参数数量不等于泛型类型定义的数量。
有办法解决这个问题吗?