相关疑难解决方法(0)

带有C#参数的'UserControl'构造函数

叫我疯了,但我喜欢那种喜欢带参数的构造函数(如果需要)的人,而不是没有参数的构造函数,后跟设置属性.我的思考过程:如果需要实际构造对象的属性,它们应该进入构造函数.我有两个好处:

  1. 我知道当构造一个对象(没有错误/异常)时,我的对象是好的.
  2. 它有助于避免忘记设置某个属性.

这种心态开始在形式/用户控制开发方面受到伤害.想象一下UserControl:

public partial class MyUserControl : UserControl
{
  public MyUserControl(int parm1, string parm2)
  {
    // We'll do something with the parms, I promise
    InitializeComponent();
  }
}
Run Code Online (Sandbox Code Playgroud)

在设计时,如果我将其UserControl放在表单上,​​我得到一个Exception:

无法创建组件'MyUserControl'...
System.MissingMethodException - 没有为此对象定义的无参数构造函数.

对我来说,似乎唯一的办法是添加默认构造函数(除非其他人知道某种方式).

public partial class MyUserControl : UserControl
{
  public MyUserControl()
  {
    InitializeComponent();
  }

  public MyUserControl(int parm1, string parm2)
  {
    // We'll do something with the parms, I promise
    InitializeComponent();
  }
}
Run Code Online (Sandbox Code Playgroud)

不包括无参数构造函数的重点是避免使用它.我甚DesignMode至无法使用该属性执行以下操作:

public partial class MyUserControl : UserControl …
Run Code Online (Sandbox Code Playgroud)

c# parameters user-controls constructor winforms

97
推荐指数
4
解决办法
5万
查看次数

在继承的表单上打开设计器时出错

我在Windows CE应用程序中打开继承的表单时遇到问题.这是一个我从前雇员那里接管的项目,但是在某些移动设备上运行了编译版本,因此我认为它应该能够打开.我有正确的VS版本(2008),并尝试清理解决方案并重建解决方案.部署解决方案时,它就像一个魅力.一旦我尝试进入继承表单的设计器,我会收到以下错误:

To prevent possible data loss before loading the designer, the following errors must be resolved:   

Object reference not set to an instance of an object. 
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

at MyApp.frmBase.UpdateOnline() in C:\Users\Corne\Documents\Visual Studio 2008\Projects\Test\MyApp\MyApp\frmBase.cs:line 35
at MyApp.frmBase.frmBase_Load(Object sender, EventArgs e) in C:\Users\Corne\Documents\Visual Studio 2008\Projects\Test\MyApp\MyApp\frmBase.cs:line 30
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Design.DesignerFrame.Initialize(Control view)
at System.Windows.Forms.Design.DocumentDesigner.Initialize(IComponent component)
at System.Windows.Forms.Design.FormDocumentDesigner.Initialize(IComponent component)
at System.ComponentModel.Design.DesignerHost.AddToContainerPostProcess(IComponent component, String name, IContainer containerToAddTo) …
Run Code Online (Sandbox Code Playgroud)

c# windows-ce windows-forms-designer

5
推荐指数
1
解决办法
3961
查看次数