Status of WinForms in NativeAOT
Today I would like to share my understanding of the status of WinForms and what’s left. I hope that WinForms would be working by that time, but unfortunately this is not yet happens. I would like to brag a lot how WinForms is usable, but unfortunately current form has it’s limitations.
Currently WinForms successfully compiles and runs WinForms which use supported controls. Other controls like DataGridView may or may not works. I simply did not test them. Overall average size of application is 37Mb for me. That’s moderately good in my opinion. Startup time not excellent, which will be improved hopefully, I have no idea how inter-tangled WinForms codebase. If you remember it was done at a time when Microsoft provide holistic solutions for customers which are magically works most of the time. Because of that there cases like
PictureBox bring dependency on
WebRequest and friends.
WebRequest is what we have prior to
HttpClient and most codebases right now try not use that class.
Some marshaling not fully implemented, for example VARIANT support and having
[MarshalAs(PreserveSig=false) and methods which return non-void type.
CoClass support not there and likely would be not implemented which is broke file dialogs, but that’s should be replaced in WinForms repo, so probably not big deal.
What’s needed for NativeAOT support for WinForms? You should provide custom implementation of ComWrappers which will take care of COM marshaling required for WinForms.
// Line what's needed.
ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance);// Regular WinForms stuff.
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1());
WinFormsComInterop is experimental implementation of ComWrappers from here, which you can add to your project and start playing with NativeAOT. I would suggest not look at that package too close, since I’m embarrased how much duck typing there, but at least you can start playing with tech. Also I promise to publish that as Nuget package, just busy for now, to properly package and publish.
Stop thinking that WinForms not supported in NativeAOT. It is barely supported! Play! Have nerd fun!