MouseDown 和定时器的组合
combination of MouseDown and timer
我尝试在触摸 (MouseDown) 和最多 1 秒期间加载类似 "holdFunction" 的功能。
所以当用户尝试触摸并按住一秒钟时,我必须调用该函数,这与 mouseUp 无关。
也许我必须结合这些:
private DateTime dtHold;
private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e)
{
dtHold = DateTime.Now;
}
private void EditProduct_MouseUp(object sender, MouseButtonEventArgs e)
{
TimeSpan interval = TimeSpan.FromSeconds(1);
if (DateTime.Now.Subtract(dtHold) > interval)
{
//HoldFunction();
}
}
和
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0,1,0);
dispatcherTimer.Start();
}
private int _sec = 0;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
_sec = _sec + 1;
if (_sec == 2)
{
dispatcherTimer.Stop();
{
//HoldFunction();
}
_sec = 0;
return;
}
}
这是您要找的吗?
如果用户按住 MouseDown
1 秒,事件将被触发?
public partial class Window2 : Window
{
private DispatcherTimer _DispatcherTimer = new DispatcherTimer();
public Window2()
{
InitializeComponent();
MouseDown += _MouseDown;
MouseUp += _MouseUp;
_DispatcherTimer.Interval = TimeSpan.FromSeconds(1.0);
_DispatcherTimer.Tick += _DispatcherTimer_Tick;
}
private void _DispatcherTimer_Tick(object sender, EventArgs e)
{
_DispatcherTimer.Stop();
Title = DateTime.Now.ToString();
}
private void _MouseUp(object sender, MouseButtonEventArgs e)
{
_DispatcherTimer.Stop();
}
private void _MouseDown(object sender, MouseButtonEventArgs e)
{
_DispatcherTimer.Start();
}
}
我尝试在触摸 (MouseDown) 和最多 1 秒期间加载类似 "holdFunction" 的功能。
所以当用户尝试触摸并按住一秒钟时,我必须调用该函数,这与 mouseUp 无关。
也许我必须结合这些:
private DateTime dtHold;
private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e)
{
dtHold = DateTime.Now;
}
private void EditProduct_MouseUp(object sender, MouseButtonEventArgs e)
{
TimeSpan interval = TimeSpan.FromSeconds(1);
if (DateTime.Now.Subtract(dtHold) > interval)
{
//HoldFunction();
}
}
和
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private void EditProduct_MouseDown(object sender, MouseButtonEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0,1,0);
dispatcherTimer.Start();
}
private int _sec = 0;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
_sec = _sec + 1;
if (_sec == 2)
{
dispatcherTimer.Stop();
{
//HoldFunction();
}
_sec = 0;
return;
}
}
这是您要找的吗?
如果用户按住 MouseDown
1 秒,事件将被触发?
public partial class Window2 : Window
{
private DispatcherTimer _DispatcherTimer = new DispatcherTimer();
public Window2()
{
InitializeComponent();
MouseDown += _MouseDown;
MouseUp += _MouseUp;
_DispatcherTimer.Interval = TimeSpan.FromSeconds(1.0);
_DispatcherTimer.Tick += _DispatcherTimer_Tick;
}
private void _DispatcherTimer_Tick(object sender, EventArgs e)
{
_DispatcherTimer.Stop();
Title = DateTime.Now.ToString();
}
private void _MouseUp(object sender, MouseButtonEventArgs e)
{
_DispatcherTimer.Stop();
}
private void _MouseDown(object sender, MouseButtonEventArgs e)
{
_DispatcherTimer.Start();
}
}