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.DisplaySettingsChanged
and after a short delay, repost WM_DISPLAYCHANGE
to 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.
class NativeRotationFix
{
private readonly Window window;
private const string MessageWindowTitle = "SystemResourceNotifyWindow";
private const uint WM_DISPLAYCHANGE = 0x007E;
private const int Delay = 500;
private int width;
private int height;
private int depth;
public delegate bool WNDENUMPROC(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumThreadWindows(uint dwThreadId, WNDENUMPROC lpfn, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
public NativeRotationFix(Window window)
{
this.window = window;
SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;
}
~NativeRotationFix()
{
SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged;
}
private void OnDisplaySettingsChanged(object sender, EventArgs e)
{
new DispatcherTimer(TimeSpan.FromMilliseconds(Delay), DispatcherPriority.Normal, (s, a) =>
{
WindowInteropHelper interopHelper = new WindowInteropHelper(window);
Screen screen = Screen.FromHandle(interopHelper.Handle);
width = screen.Bounds.Width;
height = screen.Bounds.Height;
depth = screen.BitsPerPixel;
uint threadId = GetCurrentThreadId();
EnumThreadWindows(threadId, PostToNotifyWindow, IntPtr.Zero);
(s as DispatcherTimer).Stop();
}, Dispatcher.CurrentDispatcher);
}
private bool PostToNotifyWindow(IntPtr hwnd, IntPtr lparam)
{
StringBuilder buffer = new StringBuilder(MessageWindowTitle.Length + 1);
if (GetWindowText(hwnd, buffer, buffer.Capacity) <= 0) return true;
if (buffer.ToString() != MessageWindowTitle) return true;
PostMessage(hwnd, WM_DISPLAYCHANGE, new IntPtr(depth), new IntPtr(MakeLong(width, height)));
return false;
}
private static int MakeLong(int low, int high)
{
return (int)((ushort)low | (uint)high << 16);
}
}