C# 自定义控件导致 CS0436 警告和设计器不显示
我正在开发一个项目,用于制作帖子并在其上打印任务。只是为了学习语言。我想将所有内容放入类中,并从这些类中构建项目。
因此,首先我在单独的文件中创建了一个名为 postit 的类。该文件是通过 VS2019 以某种方式导入到项目中的,因此我能够在工具箱中看到该控件。我可以很好地拖放便利贴,但一段时间后,设计器突然崩溃了。
如果我在调试器模式下运行代码并且其行为符合预期(选择、移动等),则代码运行良好。但是 VS2019 的设计器在以下错误消息中显示了一堆抱怨“错误 HRESULT E_FAIL 已从对 COM 组件的调用中返回。”,并在设计器所在的位置显示警告 CS0436代码调用类。据我了解,这是一种类型不匹配警告。这对我来说毫无意义,因为我只创建了一个地方,其中创建了名为 postit 的类型。这是如何运作的?
这是我的类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace Digipi.CustomControls
{
public class Postit : Panel
{
//Fields
public Color BGColor = ColorTranslator.FromHtml("#FFFFAA");
private Color HoverColor = ColorTranslator.FromHtml("#FFAAAA");
private Color LineColor = ColorTranslator.FromHtml("#AAAAFF");
private int startLineX = 10;
private int startLineY = 20;
public int lineInterval = 2;
public Color borderColor =Color.Black;
public int nrLines=5;
public int SelectionThickness =10;
private Point MouseDownLocation;
private bool moving = false;
private bool selected = false;
//Properties
[Category("PostIt adjustments")]
public Color BackgroundColor
{
get
{
return BGColor;
}
set
{
BGColor = value;
this.Invalidate();
}
}
[Category("PostIt adjustments")]
public int SelectionBorder
{
get
{
return SelectionThickness;
}
set
{
SelectionThickness = value;
this.Invalidate();
}
}
//Constructor
public Postit()
{
MinimumSize = new Size(50, 10);
BackColor = BGColor;
}
//Methods
private GraphicsPath Outline(bool border, float thickness=0)
{
GraphicsPath GP = new GraphicsPath();
GP.StartFigure();
GP.AddLine(thickness,thickness,this.Width-thickness,thickness);
GP.AddLine(this.Width - thickness, thickness, this.Width - thickness, this.Height - thickness);
GP.AddLine(this.Width - thickness, this.Height - thickness,thickness,this.Height-thickness);
GP.CloseFigure();
return GP;
}
protected override void OnClick(EventArgs e)
{
//base.OnClick(e);
this.selected = true;
/*foreach pi in this.Parent.Controls{
}*/
this.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
//base.OnMouseDown(e);
if (this.moving)
{
return;
}
this.moving = true;
this.MouseDownLocation = e.Location;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (!this.moving)
{
return;
}
//Point p = new Point(Cursor.Position.X - this.OffsetPoint.X, Cursor.Position.Y - this.OffsetPoint.Y);
this.Left = e.X+this.Left-MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.moving = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(Color.Transparent);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Brush b = new SolidBrush(BGColor);
e.Graphics.FillPath(b, Outline(false));
Pen pLine = new Pen(LineColor, 1);
Pen pBorder = new Pen(borderColor, SelectionThickness);
int lines = (int)(this.Height - startLineY )/ lineInterval;
for (int i = 0; i < lines; i++)
{
e.Graphics.DrawLine(pLine, startLineX, startLineY+i*lineInterval, (float)(this.Width * 0.9), startLineY + i * lineInterval);
}
if (this.selected){
e.Graphics.DrawPath(pBorder, Outline(true, SelectionThickness / 2));
}
}
}
}
插入对象是通过设计器拖放完成的,因此这是标准的。
实在不明白设计师为啥这么崩溃。。
i am working on a project to make postits and print tasks on them. Just for the sake of learning the language. I want to put everything in classes, and build the project up, from these classes.
So, to start I created a class called postit in a seperate file. The file is imported in the project somehow by VS2019, so I am able to see the control in the toolbox. I can drag and drop the postit just fine, but then after some time, out of nowhere, the designer crashes.
The code runs fine if I run it in debugger mode and behaives as expected (selecting, moving etc). but the designer from VS2019 is showing a bunch of complains in the following error message "Error HRESULT E_FAIL has been returned from a call to a COM component." and showing the warning CS0436 on the location where the designer code calls the class. This is a sort of type mismatch warning as I understood. Which makes no sense to me, since I created only one place where a type called postit is created. How does this work?
Here is my code of the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace Digipi.CustomControls
{
public class Postit : Panel
{
//Fields
public Color BGColor = ColorTranslator.FromHtml("#FFFFAA");
private Color HoverColor = ColorTranslator.FromHtml("#FFAAAA");
private Color LineColor = ColorTranslator.FromHtml("#AAAAFF");
private int startLineX = 10;
private int startLineY = 20;
public int lineInterval = 2;
public Color borderColor =Color.Black;
public int nrLines=5;
public int SelectionThickness =10;
private Point MouseDownLocation;
private bool moving = false;
private bool selected = false;
//Properties
[Category("PostIt adjustments")]
public Color BackgroundColor
{
get
{
return BGColor;
}
set
{
BGColor = value;
this.Invalidate();
}
}
[Category("PostIt adjustments")]
public int SelectionBorder
{
get
{
return SelectionThickness;
}
set
{
SelectionThickness = value;
this.Invalidate();
}
}
//Constructor
public Postit()
{
MinimumSize = new Size(50, 10);
BackColor = BGColor;
}
//Methods
private GraphicsPath Outline(bool border, float thickness=0)
{
GraphicsPath GP = new GraphicsPath();
GP.StartFigure();
GP.AddLine(thickness,thickness,this.Width-thickness,thickness);
GP.AddLine(this.Width - thickness, thickness, this.Width - thickness, this.Height - thickness);
GP.AddLine(this.Width - thickness, this.Height - thickness,thickness,this.Height-thickness);
GP.CloseFigure();
return GP;
}
protected override void OnClick(EventArgs e)
{
//base.OnClick(e);
this.selected = true;
/*foreach pi in this.Parent.Controls{
}*/
this.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
//base.OnMouseDown(e);
if (this.moving)
{
return;
}
this.moving = true;
this.MouseDownLocation = e.Location;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (!this.moving)
{
return;
}
//Point p = new Point(Cursor.Position.X - this.OffsetPoint.X, Cursor.Position.Y - this.OffsetPoint.Y);
this.Left = e.X+this.Left-MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.moving = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(Color.Transparent);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Brush b = new SolidBrush(BGColor);
e.Graphics.FillPath(b, Outline(false));
Pen pLine = new Pen(LineColor, 1);
Pen pBorder = new Pen(borderColor, SelectionThickness);
int lines = (int)(this.Height - startLineY )/ lineInterval;
for (int i = 0; i < lines; i++)
{
e.Graphics.DrawLine(pLine, startLineX, startLineY+i*lineInterval, (float)(this.Width * 0.9), startLineY + i * lineInterval);
}
if (this.selected){
e.Graphics.DrawPath(pBorder, Outline(true, SelectionThickness / 2));
}
}
}
}
Inserting the object is done via the designer drag and drop, so that is standard.
I really dont understand why the designer is crashing..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论