WPF 平板上按鈕點選不觸發,滑鼠點選觸發的兩種解決方法
阿新 • • 發佈:2019-01-07
原文:
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(); }
很簡單就解決了。