为了
解决在多个触发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();该方法将迫使界面刷新,和问
题解决。
以上是本文的全部内容,希望能对您有所帮助,希望大家多多
支持。