WPF 平板上按钮点击不触发,鼠标点击触发的两种解决方法

今天运行在windows平板上的程序,有个功能是弹出子窗体,点弹出窗体的关闭按钮,要点好几次才能触发。网上找了找,也有人与我类似的情形。

解决方法如下:

public static void DisableWPFTabletSupport()
        {
            // Get a collection of the tablet devices for this window.  
            TabletDeviceCollection devices = System.Windows.Input.Tablet.TabletDevices;

            if (devices.Count > 0)
            {
                // Get the Type of InputManager.
                Type inputManagerType = typeof(System.Windows.Input.InputManager);

                // Call the StylusLogic method on the InputManager.Current instance.
                object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
                            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                            null, InputManager.Current, null);

                if (stylusLogic != null)
                {
                    //  Get the type of the device class.
                    Type devicesType = devices.GetType();

                    // Loop until there are no more devices to remove.
                    int count = devices.Count + 1;

                    while (devices.Count > 0)
                    {
                        // Remove the first tablet device in the devices collection.
                        devicesType.InvokeMember("HandleTabletRemoved", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, devices, new object[] { (uint)0 });

                        count--;

                        if (devices.Count != count)
                        {
                            throw new Win32Exception("Unable to remove real-time stylus support.");
                        }
                    }
                }
            }
        }

这种方式,是禁用掉wpf对平板功能的支持,我试了下,确实点击关闭按钮很容易触发。但是这个方法会把滑动效果禁掉。比如滑动列表。这样用户体验会不好。

后来想了想,触摸屏点击肯定会触发TouchDown事件的。

   private void Button_TouchDown(object sender, TouchEventArgs e)
        {
            this.Close();
        }

很简单就解决了。

原文地址:https://www.cnblogs.com/czly/p/10222528.html