dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.08k stars 1.17k forks source link

ElementHost in WinForms App doesn't render its assigned Child. #7478

Closed KlausLoeffelmann closed 1 year ago

KlausLoeffelmann commented 1 year ago

Porting a small WinForms demo app from .NET Framework to .NET (Core), which uses an ElementHost.

ElementHostDemo.zip

Actual behavior:

It doesn't render the XAML Control assigned to the Child property.

Expected behavior:

It's supposed to render the XAML Control like in the Framework App:

image

Minimal repro:

See attached demo project.

Note: We (WinForms Team) are working to enable the ElementHostDesigner for the Out-of-Process WinForms Designer, when we notice the behavior.

Maybe I am missing something here, since there aren't any additional exceptions.

ThomasGoulet73 commented 1 year ago

Hey @KlausLoeffelmann, I took a quick look at your demo and it looks like there is a bug in the WinForms generated code in Form1.Designer.cs. It looks like in the .Net version of your demo, the field xamlUserControl is never initialized which means that it just sets the Child property of the ElementHost to null.

Also, there is also a bug in your demo. In the .Net version, the constructor of UserControl1 never calls the generated InitializeComponent method, which means that the Xaml is never loaded so the button is never created.

I hope this helps.

KlausLoeffelmann commented 1 year ago

InitializeComponent in the UserControl was missing - that was the actual problem. I had tried several things before I zipped the repro project, so the UserControl got instantiated in the attempts I did - but damn: I completely missed the InitializeComponent in the XAML UserControl.

My bad! 🤦

Thanks so much for taking that quick look - I would probably never guessed to look at the Xaml Control itself of all spots.

And I of all people should be familiar with InitializeComponent! 😄