我試圖解決電容式觸摸屏的靈敏度問題,如果用戶的手指通過太靠近屏幕表面,WPF按鈕將被觸發。解決WPF觸摸屏靈敏度的方法
這個問題是,許多用戶手指或手指的部分,除了他們的主要觸摸手指,靠近屏幕表面,並導致觸發不正確的按鈕。
調整屏幕的靈敏度似乎沒什麼區別,我想我可以嘗試修改按鈕按下的事件,只有按下按鈕超過一定的時間纔會觸發點擊。
任何人都可以解釋如何創建一個自定義按鈕,觸發點擊事件之前將有一個可調'按'時間。
如果可能的話,也許你會友好地包含一個非常簡單的C#/ WPF應用程序與這樣的自定義按鈕。
編輯
OK,所以我用下面的代碼,按@ kidshaw的回答創建一個子類的按鈕,但我想我必須失去了一些東西,因爲沒有獲取調用除了默認的Click事件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace AppName
{
public class TouchButton : Button
{
DoubleAnimationUsingKeyFrames _animation;
public static readonly DependencyProperty DelayElapsedProperty =
DependencyProperty.Register("DelayElapsed", typeof(double), typeof(TouchButton), new PropertyMetadata(0d));
public static readonly DependencyProperty DelayMillisecondsProperty =
DependencyProperty.Register("DelayMilliseconds", typeof(int), typeof(TouchButton), new PropertyMetadata(100));
public double DelayElapsed
{
get { return (double)this.GetValue(DelayElapsedProperty); }
set { this.SetValue(DelayElapsedProperty, value); }
}
public int DelayMilliseconds
{
get { return (int)this.GetValue(DelayMillisecondsProperty); }
set { this.SetValue(DelayMillisecondsProperty, value); }
}
private void BeginDelay()
{
this._animation = new DoubleAnimationUsingKeyFrames() { FillBehavior = FillBehavior.Stop };
this._animation.KeyFrames.Add(new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), new CubicEase() { EasingMode = EasingMode.EaseIn }));
this._animation.KeyFrames.Add(new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(this.DelayMilliseconds)), new CubicEase() { EasingMode = EasingMode.EaseIn }));
this._animation.Completed += (o, e) =>
{
this.DelayElapsed = 0d;
//this.Command.Execute(this.CommandParameter); // Replace with whatever action you want to perform
Console.Beep();
this.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
};
this.BeginAnimation(DelayElapsedProperty, this._animation);
}
private void CancelDelay()
{
// Cancel animation
this.BeginAnimation(DelayElapsedProperty, null);
}
private void TouchButton_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
{
this.BeginDelay();
}
private void TouchButton_TouchUp(object sender, System.Windows.Input.TouchEventArgs e)
{
this.CancelDelay();
}
}
}
如何調用TouchButton_TouchDown方法?難道我不得不以某種方式將它分配給TouchDown偶數處理程序嗎?
好的,我添加了一個構造函數,並設置TouchDown/Up事件處理程序,使其可以工作,但CancelDelay()不會阻止事件被觸發。它似乎工作正常,並在用戶擡起手指時被調用,但不會阻止事件被觸發。
想不出你創建一個用戶控件一個按鈕,wh ich在按下時啓動一個定時器,並在100ms後手動觸發點擊事件? – 2014-09-04 05:46:48
那麼問題是,Windows觸摸驅動程序似乎沒有區分輕輕觸摸或長時間觸摸。所以我需要按鈕才能觸發事件,如果長按按鈕(並且需要一些實驗來確定可用性的最佳持續時間)。我的猜測是印刷機的時間大約是200-300毫秒,而傳球的觸發器會少於200.我想我可以在Press上啓動定時器並在Release上停止,如果> X觸發事件。 – 2014-09-04 06:22:44
多數民衆贊成我的意思;) – 2014-09-04 06:28:31