在 C# .NET 2010 中继承用户控件 [英] Inheriting user controls in C# .NET 2010

查看:32
本文介绍了在 C# .NET 2010 中继承用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WinForms 在 C# .NET 2010 中开发一个程序,但很难将 System.windows.Forms.DataVisualization.Charting.Chart 类继承到自定义图表中.

I am working on a program in C# .NET 2010 using WinForms and am having difficulty inheriting the System.windows.Forms.DataVisualization.Charting.Chart class into a custom chart.

当我在 Visual Studio 设计器中查看表单时,我收到一条错误消息:

When I view the form in the Visual Studio designer, I get an error saying:

找不到类型ExpeView.ExpeDataChart".请确保引用了包含此类型的程序集.如果此类型是您的开发项目的一部分,请确保已使用当前平台或任何 CPU 的设置成功构建项目.

Could not find type 'ExpeView.ExpeDataChart'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built using settings for your current platform or Any CPU.

文件 ExpeDataChart.cs 是这个项目的一部分,每当我编译和运行程序时,它都能完美运行.此错误仅出现在设计器视图中,使我无法通过 GUI 编辑表单.

The file ExpeDataChart.cs is part of this project, and whenever I compile and run the program, it works perfectly. This error only comes up in the designer view, making it impossible for me to edit the form through the GUI.

ExpeDataChart.cs 文件看起来像这样(这被缩小了,但可以理解):

The file ExpeDataChart.cs looks like this (this is slimmed down, but gets the point across):

namespace ExpeView
{
    public class ExpeDataChart : Chart
    {
        public ExpeDataChart()
        {
            MouseMove += SampleMouseMove;
            MouseLeave += SampleMouseLeave;
            MouseDoubleClick += SampleDoubleClick;
            MouseDown += SampleMouseDown;
            MouseUp += SampleMouseUp;
            MouseEnter += SampleMouseEnter;
            MouseLeave += SampleMouseLeave;
            MouseWheel += SampleMouseWheel;
        }

        private void SampleMouseMove(object sender, MouseEventArgs e) { }
        private void SampleMouseLeave(object sender, EventArgs e) { }
        private void SampleDoubleClick(object sender, MouseEventArgs e) { }
        private void SampleMouseDown(object sender, MouseEventArgs e) { }
        private void SampleMouseUp(object sender, MouseEventArgs e) { }
        private void SampleMouseEnter(object sender, EventArgs e) { }
        private void SampleMouseWheel(object sender, MouseEventArgs e) { }
        private void SampleZoomIn(int x) { }
        private void SampleZoomOut() { }
    }
}

什么会导致它无法在设计器中正确加载,但在编译时仍能正确加载?错误的调用栈为:

What would cause it to not properly load in the designer, but still load properly when compiling? The call stack of the error is:

ExpeView FormExpeView.Designer.cs Line:438 Column:1 atSystem.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManagermanager, String exceptionText, String helpLink) 在System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager经理、字符串名称、CodeExpression 表达式)在System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager经理、字符串名称、CodeExpression 表达式)在System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager经理,CodeStatement 语句)

ExpeView FormExpeView.Designer.cs Line:438 Column:1 at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink) at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression) at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression) at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)

推荐答案

移动文件 ExpeDataChart.cs 到一个单独的项目,一个库项目.新项目可以是 Visual C# 解决方案的一部分,也可以是单独的解决方案,例如自定义控件.然后将库项目的引用添加到使用自定义控件的项目中.现在您应该可以使用可视化设计器编辑自定义控件了.

Move the file ExpeDataChart.cs to a separate project, a library project. The new project can be part of the Visual C# solution, or a separate solution, e.g. CustomControls. Then add a reference to the library project to the project that uses the custom control. Now you should be able to edit the custom control using the visual designer.

这篇关于在 C# .NET 2010 中继承用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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