简体   繁体   中英

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#).

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:

  1. Create a Smart Device class library project.
  2. Add a new class ListViewEx : System.Windows.Forms.ListView
  3. Override the OnHandleCreated method:

     protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); } 
  4. 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.)

  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.
  8. Add one column and one ListViewItem with arbitrary text.
  9. Set View property of the ListViewEx to 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:

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:

  1. If the OnHandleCreated is removed the control works as expected.
  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 . 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.

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