旋转我的三星平板电脑后,是什么导致 WPF 应用程序中的触摸输入被忽略?

发布于 2024-12-25 14:54:01 字数 1614 浏览 0 评论 0原文

我有一个 WPF 应用程序,正在 Samsung XE700T1A 平板电脑上测试。我没有其他平板电脑可以测试它。启动应用程序后,当我将平板电脑旋转到纵向模式时,我的 UI 元素停止响应触摸。再次旋转回横向后,他们继续忽略触摸,但第三次旋转回纵向后,触摸起作用。进一步的旋转似乎会产生不确定的结果。有时触摸有效,有时你必须不断旋转才能恢复。我发现初始序列偶尔会有变化,但大多数情况下它看起来非常一致。

为了缩小可能性范围,我创建了一个简单的 WPF 应用程序来演示该问题。下面是代码:

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();
    }
}

这个简单的演示应用程序也托管 GitHub 上

有谁知道是什么导致了这个问题?或者如何解决?任何人都可以重现这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2025-01-01 14:54:01

这显然是 Microsoft 内部已知的问题,其中“WPF 的触摸系统并不总是正确检测分辨率变化。

解释那里的评论,解决方法是订阅SystemEvents.DisplaySettingsChanged 并在短暂延迟后,将 WM_DISPLAYCHANGE 重新发布到当前线程中标题为“SystemResourceNotifyWindow”的窗口。

然而,那里发布的实际代码对于 Microsoft 以外的任何人来说都不实用。下面的代码适用于我们其他人。我已经在 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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文