I am writing an extension for standard ListView control in compact framework (VS2008, target platform - Windows Mobile 6.1, C#).
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.
Steps to reproduce:
ListViewEx : System.Windows.Forms.ListView
Override the OnHandleCreated
method:
protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); }
Add the file DesignTimeAttributes.xmta
to the project:
<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.)
ListBoxEx
on the Form1
from the toolbox. ListViewItem
with arbitrary text. View
property of the ListViewEx
to Details
. At this point I would expect to see my list view with one item in it. Instead VS throws an error:
The control Microsoft.CompactFramework.Design.HeaderControl has thrown an unhandled exception in the designer and has been disabled.
Exception:
InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index
Stack trace:
at Microsoft.CompactFramework.Design.HeaderControl.OnPaint(PaintEventArgs e)
at Microsoft.CompactFramework.Design.HeaderControl.WndProc(Message& m)
When I press OK
, the ListViewEx placeholder looks like this
Furthermore, if I leave the form open for some time (about a minute) VS also throws a new error:
Code generation for property 'Items' failed. Error was: 'InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index'
Some notes:
OnHandleCreated
is removed the control works as expected. Now, I understand that the obvious workaround solution would be to avoid overriding the OnHandleCraeted
. But I want to execute some native windows methods that require HWND handle as a part of initialization routine. And OnHandleCreated
seems to be the right place to do so. (The control fails with only having base.OnHandleCreated
in the body of the method)
So the question is: is there a way to keep the OnHandleCreated
override and make the control work in design time? Or another one: when and where is the best to execute native windows methods which require HWND handle?
Well, I managed to get things working. I've put my initialization to OnParentChanged
. I know it may get called more than once and I got i covered.
Smells like a dirty hack though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.