Description
openedon Feb 27, 2024
Environment
Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.8.6
.NET version
.NET 8, happens in .NET 6 as well
Did this work in a previous version of Visual Studio and/or previous .NET release?
This works correctly in .NET Framework.
Issue description
,NET Core designer does not respect LicenseManager.UsageMode
to be LicenseUsageMode.Designtime
.
This is very important so that code in constructor and OnLoad
event handler can be selected which has to be skipped when designer executes the UserControl
.
Code in OnLoad
handler can probably be solved by Component.DesignMode
(I have a note from older development that Component.DesignMode
does not work inside constructor - from https://stackoverflow.com/a/1774719/3625699) .
Consequences are seen when you develop a UserControl
which uses external dependencies in constructor body which are not available during design time and this UserControl
is placed into a Form
.
Steps to reproduce
I created a full reproduction solutio nwhich compares .NET Framework and .NET Core designer behavior.
https://github.com/janseris/WinFormsDesignerModeTest - try add the UserControl
from ToolBox in the _Core
project into the Window.
Detailed universal description:
Create a UserControl.
Add code which references external dependencies which are provided by the application in UserControl constructor. They are not available during design time. This code needs to be skipped when in constructor because it will crash.
For example any code that uses Service Locator antipattern. This antipattern has to be used in UserControls because their instances cannot be managed by DI container if you wish to see them in Windows Forms designer.
Now compile the project - that shows the latest version of the UserControl available in ToolBox and try placing the UserControl into the Form (MainWindow).
Diagnostics
No response
P.S.: It would be also very helpful if the line and file (in my code, not in DesignToolsServer) where the error happened, was printed. I have otherwise no idea where the NullReferenceException or any other issue comes from.
Example: MainWindow.cs, line 2