How to set PerMonitor DpiAwareness for .NET 8 Windows desktop application in WPF?
Hi,
I am having problem setting WPF window based desktop .NET 8 application to work as PerMonitor DpiAware.
System default automatic scaling schema...
- When user changes Windows 10 display setting scale, WPF-Window is being scaled accordingly.
- Also, when window is dragged to another display, that has different scale, window is being scaled.
I want to prevent these automatic scaling because I'm using my own scaling schema.
Before .NET 6, when application was using WPF 4.8, following two items together were preventing such automatic behavior:
A) @ app.manifest
...
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
</windowsSettings>
</application>
...
B) @ app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
<runtime>
<AppContextSwitchOverrides value="Switch.System.Windows.DoNotScaleForDpiChanges=true"/>
</runtime>
</configuration>
Now, @ .NET 8, that is not working anymore!
After a lots of hard work finally I find the solution...trick was ... app.config file is not NET 8 compatible.
So, AppContextSwitchOverrides … Switches like...
Switch.System.Windows.DoNotScaleForDpiChanges
are transferred from
"appname".config => "appname".runtimeconfig.json
In order to control these switches ... I had to put new item group into *.csproj
<Project>
...
<ItemGroup>
<RuntimeHostConfigurationOption Include="Switch.System.Windows.DoNotScaleForDpiChanges" Value="true" />
<RuntimeHostConfigurationOption Include="Switch.System.IO.UseLegacyPathHandling" Value="false" />
</ItemGroup>
...
</Project>
So, now the PerMonitor DpiAwareness is working as expected. But there a little bit problem arise, When I start my Application on a different monitor which have different screen resolution and different screen scaling, my App is unable to re-scale it's UI.
But the app runs perfectly when I change the screen DPI and screen resolutions at the time of running and if I close the app and run again on that different screen DPI and screen resolution it's not working as expected.