I have a WPF app that I’m testing on a Samsung XE700T1A tablet. I have no other tablets to test it on. Upon launching the app, when I rotate the tablet to portrait mode, my UI elements stop responding to touch. They continue to ignore touch after rotating back to landscape again, but then after rotating a third time back to portrait, touch works. Further rotations seem to have indeterminate results. Sometimes touch works, sometimes you have to keep rotating to get it back. I have seen an occasional variation in that initial sequence, but for the most part it appears very consistent.
In order to narrow down the range of possibilities, I created a simple WPF app to demonstrate the issue. Here is the code:
MainWindow.xaml
<Window x:Class="RotationBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="DisplayText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="I'm happy." />
<Button Grid.Row="1"
Height="22"
Width="80"
Margin="0,0,0,20"
Click="OnButtonPress"
Content="Depress Me" />
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnButtonPress(object sender, RoutedEventArgs e)
{
DisplayText.Text = DateTime.UtcNow.Ticks.ToString();
}
}
This simple demo app is also hosted here on GitHub.
Does anyone know what is causing this issue? Or how to resolve it? Can anyone reproduce this issue?
This is apparently an issue known internally at Microsoft where “WPF’s touch system does not always detect resolution changes correctly.“
Paraphrasing comments there, the workaround is to subscribe to
SystemEvents.DisplaySettingsChangedand after a short delay, repostWM_DISPLAYCHANGEto the window titled “SystemResourceNotifyWindow” in the current thread.However, the actual code posted there is not practical for anyone outside Microsoft. The code below will work for the rest of us. I’ve posted a sample solution here on GitHub.