dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.42k stars 984 forks source link

ParentControlDesigner/ComponentDesigner is not instantiated/initialized #11531

Closed CortiWins closed 5 months ago

CortiWins commented 5 months ago

Environment

Version 17.10.2

.NET version

.Net 8.0

Did this work in a previous version of Visual Studio and/or previous .NET release?

Net Framework 4.8 and earlier

Issue description

I tracked down issues with much more complicated controls (including crashing visual studio) to the minimum example attached. This exact code works in Net4.8 but does not in Net8/7. The designer class derived fromParentControlDesigner is neither instanced nor initialized.

The UserControl

[System.ComponentModel.Designer(typeof(TestUserControlDesigner))]
public partial class TestUserControl : UserControl
{
    public TestUserControl()
    {
        InitializeComponent();
    }
    [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
    public Panel ContentPanel => this.pnlContent;
}

The Designer

public class TestUserControlDesigner : System.Windows.Forms.Design.ParentControlDesigner
{
    public TestUserControlDesigner()
    {
        MessageBox.Show("Create NetCore TestUserControlDesigner");
    }

    public override void Initialize(System.ComponentModel.IComponent component)
    {
        MessageBox.Show("Initialize NetCore TestUserControlDesigner");
        base.Initialize(component);
        if (this.Control is TestUserControl group)
        {
            this.EnableDesignMode(group.ContentPanel, "EditablePanel");
        }
    }
}

This solution contains projects for net4.8 and Net8 for a direct comparison UserControlDesignerIsNotCalledExample.zip

Steps to reproduce

The attached project contains the code to reproduce the discreprancy between net4.8 and Net8.

Diagnostics

No response

elachlan commented 5 months ago

@Olina-Zhang can your team please test this?

Zheng-Li01 commented 5 months ago

@CortiWins this issue can be resolved using Nuget: Microsoft.WinForms.Designer.SDK, and need to refactor the namespaces from System.Windows.Forms.Design.ParentControlDesigner to Microsoft.DotNet.DesignTools.Designers.ParentControlDesigner, referring to blog: https://devblogs.microsoft.com/dotnet/custom-controls-for-winforms-out-of-process-designer/ GHIssue11531

elachlan commented 5 months ago

@KlausLoeffelmann maybe we should add an analyzer for this?

CortiWins commented 5 months ago

Okay, so using Microsoft.WinForms.Designer.SDK solved it for me, but required one more change to the codebase.

And for future searchers and finders of this topic: While the Assembly containing the System.Windows.Forms.Design.ParentControlDesigner is available during runtime, the assembly that contains Microsoft.DotNet.DesignTools.Designers.ParentControlDesigner is not.

This means, that Reflection that resolves all included Type-dependencies ( like Assembly.GetTypes() ) on the assembly that contains the designer-class will now lead to a "file not found" exception during runtime.

merriemcgaw commented 5 months ago

I think we'd want to put this analyzer in our Designer SDK rather than here, so @Olina-Zhang can you please take a moment to create an issue in the designer repo to track the suggestion if we don't have it already?

Zheng-Li01 commented 4 months ago

@merriemcgaw filed GH DT https://github.com/microsoft/winforms-designer/issues/6002 to track this.