在WPF按钮点击点击多次在同一时间

在WPF按钮点击点击多次在同一时间
为了解决在多个触发Click方法同时点击WPF按钮,供你参考,以下具体内容
日期时间lastclick =现在的日期时间;
对象=新的对象();
int = i 0;
private void button_click(object sender,routedeventargs E)
{
this.isenabled = false;
var t =(datetime.now - TotalMilliseconds lastclick);
++;
现在lastclick =的日期时间;
system.diagnostics.debug.print(T +
线程(睡眠)(2000);
this.isenabled =真;
}
上面的代码并没有解决用户点击两个按钮触发两倍的问题,因为UI线程是单线程的,所以这会导致用户点击两次连续,然后调用button_click两秒。

1207.069,1;13:58:22,2017年4月19日

2055.1176,2;13:58:24,2017年4月19日
所以在this.isenabled = false;在强制刷新界面,代码如下:
private void button_click(object sender,routedeventargs E)
{
this.isenabled = false;
dispatcherhelper.doevents();
var t =(datetime.now - TotalMilliseconds lastclick);
++;
现在lastclick =的日期时间;
system.diagnostics.debug.print(T +
线程(睡眠)(2000);
this.isenabled =真;
}
公共静态类dispatcherhelper
{
{ securitypermissionattribute(securityaction.demand,旗帜= securitypermissionflag。unmanagedcode)}
public static void DoEvents()
{
dispatcherframe框架=新dispatcherframe();
dispatcher.currentdispatcher.begininvoke(dispatcherpriority.background,新dispatcheroperationcallback(exitframes),框架);
尝试{ dispatcher.pushframe(架);}
赶上(InvalidOperationException){ }
}
私有静态对象ExitFrames(框架)
{
((dispatcherframe)框架)。继续= false;
返回null;
}
}
dispatcherhelper.doevents();该方法将迫使界面刷新,和问题解决。

以上是本文的全部内容,希望能对您有所帮助,希望大家多多支持
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部