Repository navigation
Description
Rationale
In the WinForms 3.1-time frame, we introduced the SetHighDpiMode method and the HighDpiMode property. The reason for that was that we wanted to change the default HighDpiMode for WinForms Apps to be SystemAware. At the same time, we wanted to provide also a programmatically way to set the DpiMode, since the only remaining option to do that was over the App’s manifest – the App.Config option we were no longer supposed to use for .NET Core Apps. However, setting/changing the HighDpiMode via the manifest requires the App to be recompiled/rolled out anew, which was the reason to introduce the API as an alternative. Since we did not want to support the Visual Basic Application Framework in the time, we did not include new API options for the AF.
We decided however to enable the Application Framework for Visual Basic WinForms App (somewhat late) in the .NET 5-time frame. Unfortunately, it was to late to change the runtime at the time to include some necessary changes for setting the HighDpiMode programmatically in the Application Framework, so we decided to provide this event (and the setting of the correct default value) via an additional template file for Application Framework Apps, which is only active in .NET 5 Apps. We decided, we wanted to implement those events for .NET 6, and then supersede the template implementation by the actual API.
Visual Basic Application Framework Apps ever have had a different way compared to C# to configure the start-up behavior of an WinForms app: While we have Program.cs in C#, there is no equivalent in VB with the Application Framework active. Rather, typical startup behavior is configured over several Application events:
For this reason, setting the HighDpiMode shouldn’t have been an exception conceptionally, so we introduced the ApplyHighDpiMode Application event via the template.
Since then, WinForms also has the necessity to set the default Font. The reason for that is, that we changed the default Font in .NET 3.1. That had an impact on pixel perfect Form designs, which were based on the previous Font settings. To accommodate User request for migration projects not needing to redesign all the Forms which based on the original Font, we decided to introduce the SetDefaultFont method for .NET 6. Again, this setting needs to get applied to the Application object, and since – again – in an AF App there is no Program.cs, we want to introduce an additional Event to set this default value. However, in contrast to C#, we need to make sure not to spam the Event list with yet additional Application events, which is why we decided to take a more holistic approach for AF by introducing the ApplyApplicationDefaults event for the AF. Now, with it, we will introduce a few new Properties to set as application wide default values, but unfortunately also a breaking change: We want to phase out ApplyHighDpiMode!
So, we will have in the first go of the Default values:
- Font - will be setting the DefaultFont.
- HighDpiMode - will be setting the HighDpiMode and replace the template-implemented ApplyHighDpiMode Application event. This is only a breaking change, if folks have actually set this value via the Application Events.
- MinimumSplashScreenDisplayTime - it's a valuable property to set, but could previously only be set by overwriting
OnInitializeof theWindowsFormsApplicationBaseclass. Customers shouldn't have such a hard time to discover how to set this value.
Since we’re sure to add more default properties in the future, this EventArgs class can easily be extended to accommodate more default settings (like Margin, Padding, default values for right-to-left settings, Default Form settings and more.) For the methodology to set default values, we decided that we do not need to be in sync with C#, and the VB Application Framework can have its own identity.
Actual APIs to be added:
This comes together in WindowsFormsApplicationBase, which will be extended by this:
Public Event ApplyApplicationDefaults As ApplyApplicationDefaultsEventHandler
''' <summary>
''' We need this, so Project System will have a chance to set this value
''' when we will implement this via a combo box for the VB Application Framework Designer.
''' </summary>
Protected Property HighDpiMode() As Windows.Forms.HighDpiMode
For that, we need a new EventArgs class:
Public Class ApplyApplicationDefaultsEventArgs
Inherits EventArgs
Public Property DefaultFont As Font
Public Property HighDpiMode As System.Windows.Forms.HighDpiMode
Public Property MinimumSplashScreenDisplayTime As Integer
End Class
And finally, we also need this delegate for the event:
''' <summary>
''' Signature for the ApplyApplicationDefaults event handler
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub ApplyApplicationDefaultsEventHandler(sender As Object, e As ApplyDefaultsEventArgs)
[UPDATE 7/10/2021: We will get rid of the ApplyHighDpiSetting event and unify that with the ApplyDefaultValues event by having the HighDpiMode property on the ApplyApplicationDefaultsEventArgs. We also introduce the MinimumSplashScreenDisplayTime property.]
Activity
KlausLoeffelmann commentedon Jun 9, 2021
@merriemcgaw, @RussKie, @KathleenDollard, @JeremyKuhne - please take a look!
This is the requirement for updating the .NET 6 templates.
merriemcgaw commentedon Jun 10, 2021
That sounds good to me. Thanks @KlausLoeffelmann!
[-]Introduce ApplyHighDpiMode and ApplyApplicationDefaults Events to the Application Framework[/-][+]Introduce ApplyApplicationDefaults Event to the Application Framework[/+]KlausLoeffelmann commentedon Jul 10, 2021
After reconsidering the scope of the
ApplyDefaultValuesApplication Framework event, this would be the Visual Basic experience:KathleenDollard commentedon Jul 11, 2021
I like this simplified approach, but I am concerned about the transition for folks.
Can we support the MyApplication_HighDpiMode event handler, but perhaps mark it as deprecated or otherwise let folks know to move away from it?
If MinimumSplashScreenDisplayTime is set in both the MyApplication_ApplyApplicationDefaults and the OnInitialize override, which wins?
KlausLoeffelmann commentedon Jul 12, 2021
I thought about that, too. But we'd be introducing a new API as deprecated, which strikes me as a bit peculiar.
I was thinking about an analyzer, which could help resolving that error?
Then the EventArgs would get prepopulated with the override, but can then be changed - so the event ultimately wins.
paul1956 commentedon Jul 12, 2021
@KlausLoeffelmann Mistakes happen do the right thing. The same thing happened in System.JSON with Options and they did the right thing in .Net 6.0. It was immediately obvious I made the change that works in .Net 5.0 and 6.0, I guess they backported the changed API.
KlausLoeffelmann commentedon Jul 12, 2021
I wouldn't even consider it to be a mistake: At the time, we've been introducing
ApplyHighDpiModewe hadn't thought about Default values. I had that idea only when we were start thinking of how to address the Font issue in the designer.So, am I getting you correctly: You say let's phase
ApplyHighDpiModeout, and go withApplyApplicationDefaults?paul1956 commentedon Jul 12, 2021
@KlausLoeffelmann Yes exact. If possible add the API as something I can use in an update to .Net 5 so I can have common source during the transition, Otherwise please provide an example in the error message on what I need to do to have common source with #IF. This is what happened with JsonSerializerOptions DefaultIgnoreCondition and is a great model.
terrajobst commentedon Jul 16, 2021
Since I'm not proficient in VB (and neither are most people on @dotnet/fxdc) I have written the API in C#. Please let me know if I messed it up:
terrajobst commentedon Jul 16, 2021
Looks reasonable. I'd just suggest to use a
TimeSpanforMinimumSplashScreenDisplayTimeas this removes the ambiguity if the unit is milliseconds, seconds, or fortnight per inch squared.bartonjs commentedon Jul 16, 2021
FDG says:
So I think the event just needs to be declared as
EventHandler<ApplyDefaultsEventArgs>.terrajobst commentedon Jul 16, 2021
Ah yes. Good call.
KlausLoeffelmann commentedon Jul 20, 2021
We had some problems in the past with generating the stubs for the Visual Basic
Handlesclause based on generics in Visual Basic, if I remember correctly. We should thoroughly test this, and if that works, I'll make the change!bartonjs commentedon Jul 20, 2021
Video
API Review:
ApplyApplicationDefaultsEventHandlershould beEventHandler<ApplyApplicationDefaults>, but since the rest of the events on this type are custom delegates doing this one that way is OK (and may be needed due to an IDE limitation)MinimumSplashScreenDisplayTimewould be aTimeSpan, but since it's just echoing an existing unitlessintproperty it's OK.Philip-Wang01 commentedon Aug 27, 2021
Verified the issue with 6.0.100-rc.1.21424.1 build , complete introduced ApplyApplicationDefaults Event to the Application Framework
