Skip to content

Instantly share code, notes, and snippets.

@dotMorten
Created September 4, 2015 22:31
Show Gist options
  • Save dotMorten/9ca40caf740d553f5b8c to your computer and use it in GitHub Desktop.
Save dotMorten/9ca40caf740d553f5b8c to your computer and use it in GitHub Desktop.
Logical DPI and scale changes all of a sudden
<Page
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="text" TextWrapping="WrapWholeWords" Margin="50" IsHitTestVisible="False" Grid.Row="1"/>
</Grid>
</Page>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var info = Windows.Graphics.Display.DisplayInformation.GetForCurrentView();
info.DpiChanged += Info_DpiChanged;
text.Text += "InitializeComponent: "; UpdateDisplayInfo();
DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(50) };
timer.Tick += timer_Tick;
timer.Start();
this.Loaded += MainPage_Loaded;
}
private void Info_DpiChanged(Windows.Graphics.Display.DisplayInformation sender, object args)
{
//This never fires
text.Text += "DpiChanged: "; UpdateDisplayInfo();
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
text.Text += "Loaded: "; UpdateDisplayInfo();
}
private void UpdateDisplayInfo()
{
//On HP Spectre, the display info values suddenly change sometime after 'Loaded'
var info = Windows.Graphics.Display.DisplayInformation.GetForCurrentView();
text.Text += string.Format("LogicalDpi:{0}, Scale:{1}, RawDpi:{2}\n", info.LogicalDpi, info.ResolutionScale, info.RawDpiX);
}
void timer_Tick(object sender, object e)
{
UpdateDisplayInfo();
}
}
@dotMorten
Copy link
Author

Run this code in a Universal 10 app, and in an 8.1 Windows Store app.
You'll see when running on an HP Spectra with Windows 10 (the one from Build 2015), that the output for dpi and scale suddenly changes sometime after load. If you're running the app on Windows 8.1, this doesn't happen either. We have also observed this on another PC, but for instance it can't be reproduced on Surface Pro 3. Problem seems to be worst (most off) when setting the scaling factor to 125%

The really curious thing is that if the app considers the DPI to have changed, then why did the DpiChanged event not fire?

Worst thing is, the reported values are obviously wrong (scale factor of 116% and LogicalDPI at 80).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment