[size=medium]禁用按钮是要在整个事件结束后才会响应[/size]
public class WpfApplication
{
private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
public static void DoEvents()
{
DispatcherFrame nestedFrame = new DispatcherFrame();
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
Dispatcher.PushFrame(nestedFrame);
if (exitOperation.Status != DispatcherOperationStatus.Completed)
{
exitOperation.Abort();
}
}
private static Object ExitFrame(Object state)
{
DispatcherFrame frame = state as DispatcherFrame;
frame.Continue = false;
return null;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
button.IsEnabled = false;
WpfApplication.DoEvents();
i++;
button.Content = i.ToString();
System.Threading.Thread.Sleep(2000);
button.IsEnabled = true;
}

本文介绍了一个在WPF应用程序中实现按钮禁用并延迟重新启用的方法。通过使用Dispatcher操作来确保在特定事件(如长时间运行的操作)完成后才重新启用按钮。此方法利用了DispatcherFrame和DispatcherOperation来控制UI线程的行为。
1944

被折叠的 条评论
为什么被折叠?



