简体   繁体   English

紧凑框架中自定义控件中的设计时错误

[英]Design time error in custom control in compact framework

I am writing an extension for standard ListView control in compact framework (VS2008, target platform - Windows Mobile 6.1, C#). 我正在为紧凑框架(VS2008,目标平台-Windows Mobile 6.1,C#)中的标准ListView控件编写扩展。

The class is currently pretty much empty, only having one method override (see the code below). 该类目前几乎是空的,只有一个方法重写(请参见下面的代码)。 The problem I encounter is this: when I have my control on a form with at least ListViewItem the control fails to initialize when opened in design time. 我遇到的问题是:当我将控件放在至少具有ListViewItem的窗体上时,控件在设计时打开时无法初始化。

Steps to reproduce: 重现步骤:

  1. Create a Smart Device class library project. 创建一个智能设备类库项目。
  2. Add a new class ListViewEx : System.Windows.Forms.ListView 添加一个新类ListViewEx : System.Windows.Forms.ListView
  3. Override the OnHandleCreated method: 覆盖OnHandleCreated方法:

     protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); } 
  4. Add the file DesignTimeAttributes.xmta to the project: 将文件DesignTimeAttributes.xmta添加到项目中:

     <Classes xmlns="http://schemas.microsoft.com/VisualStudio/2004/03/SmartDevices/XMTA.xsd"> <Class Name="CompleteNamespace.ListViewEx"> <DesktopCompatible>true</DesktopCompatible> <DesignTimeVisible>true</DesignTimeVisible> </Class> </Classes> 

    (Replace the CompleteNamespace with the correct namespace.) (用正确的名称空间替换CompleteNamespace 。)

  5. Build the project. 生成项目。
  6. Create a new Smart Device Application project in the same solution. 在相同的解决方案中创建一个新的智能设备应用程序项目。
  7. Drag the ListBoxEx on the Form1 from the toolbox. 从工具箱中拖动Form1上的ListBoxEx
  8. Add one column and one ListViewItem with arbitrary text. 添加一列和一个带有任意文本的ListViewItem
  9. Set View property of the ListViewEx to Details . ListViewEx View属性设置为Details
  10. Save, rebuild everything and close the form designer tab. 保存,重建所有内容并关闭“表单设计器”选项卡。
  11. Reopen form designer. 重新打开表单设计器。

At this point I would expect to see my list view with one item in it. 在这一点上,我希望看到其中包含一项的列表视图。 Instead VS throws an error: 相反,VS会引发错误:

The control Microsoft.CompactFramework.Design.HeaderControl has thrown an unhandled exception in the designer and has been disabled. 控件Microsoft.CompactFramework.Design.HeaderControl在设计器中引发了未处理的异常,并且已被禁用。

Exception: 例外:

InvalidArgument=Value of '-1' is not valid for 'index'. InvalidArgument =值“ -1”对“索引”无效。 Parameter name: index 参数名称:索引

Stack trace: 堆栈跟踪:
at Microsoft.CompactFramework.Design.HeaderControl.OnPaint(PaintEventArgs e) 在Microsoft.CompactFramework.Design.HeaderControl.OnPaint(PaintEventArgs e)
at Microsoft.CompactFramework.Design.HeaderControl.WndProc(Message& m) 在Microsoft.CompactFramework.Design.HeaderControl.WndProc(Message&m)

When I press OK , the ListViewEx placeholder looks like this 当我按OK ,ListViewEx占位符看起来像这样 代替常规控制内容显示的错误文本

Furthermore, if I leave the form open for some time (about a minute) VS also throws a new error: 此外,如果我将表单打开一段时间(大约一分钟),VS还会引发一个新错误:

Code generation for property 'Items' failed. 属性“ Items”的代码生成失败。 Error was: 'InvalidArgument=Value of '-1' is not valid for 'index'. 错误是:'InvalidArgument =值'-1'对于'索引'无效。

Parameter name: index' 参数名称:索引'

Some notes: 一些注意事项:

  1. If the OnHandleCreated is removed the control works as expected. 如果OnHandleCreated被删除,则控件将按预期工作。
  2. The control does not fail to initialize when there are no items in it. 当控件中没有任何项时,它不会失败。
  3. The control works fine in run time. 该控件在运行时工作正常。

Now, I understand that the obvious workaround solution would be to avoid overriding the OnHandleCraeted . 现在,我知道明显的解决方法是避免覆盖OnHandleCraeted But I want to execute some native windows methods that require HWND handle as a part of initialization routine. 但是我想执行一些需要HWND句柄的本机Windows方法,作为初始化例程的一部分。 And OnHandleCreated seems to be the right place to do so. OnHandleCreated似乎是正确的选择。 (The control fails with only having base.OnHandleCreated in the body of the method) (该控件失败,方法的主体中只有base.OnHandleCreated

So the question is: is there a way to keep the OnHandleCreated override and make the control work in design time? 所以问题是:有没有办法保持OnHandleCreated覆盖并使控件在设计时工作? Or another one: when and where is the best to execute native windows methods which require HWND handle? 还是另一个:什么时候最好在哪里执行需要HWND句柄的本机Windows方法?

Well, I managed to get things working. 好吧,我设法使事情顺利进行。 I've put my initialization to OnParentChanged . 我已经将初始化设置为OnParentChanged I know it may get called more than once and I got i covered. 我知道它可能会不止一次被叫到,我得到了覆盖。

Smells like a dirty hack though. 闻起来像是肮脏的骇客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM