[英]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: 重现步骤:
ListViewEx : System.Windows.Forms.ListView
ListViewEx : System.Windows.Forms.ListView
Override the OnHandleCreated
method: 覆盖
OnHandleCreated
方法:
protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); }
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
。)
ListBoxEx
on the Form1
from the toolbox. Form1
上的ListBoxEx
。 ListViewItem
with arbitrary text. ListViewItem
。 View
property of the ListViewEx
to Details
. ListViewEx
View
属性设置为Details
。 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: 一些注意事项:
OnHandleCreated
is removed the control works as expected. OnHandleCreated
被删除,则控件将按预期工作。 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.