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

很簡單就解決了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM