C# 选中和取消选中动画需要时间
当用户单击复选框时,复选框会在3秒钟后被选中,因为LoadShowGuidData()方法正在加载大量数据,我正在ShowGuid()方法中使用“this.dispatcher.BeginInvoke()”调用该方法,但该方法对解决问题无效我想要的是,用户单击后应立即选中复选框,然后加载数据。C# 选中和取消选中动画需要时间,c#,wpf,data-binding,C#,Wpf,Data Binding,当用户单击复选框时,复选框会在3秒钟后被选中,因为LoadShowGuidData()方法正在加载大量数据,我正在ShowGuid()方法中使用“this.dispatcher.BeginInvoke()”调用该方法,但该方法对解决问题无效我想要的是,用户单击后应立即选中复选框,然后加载数据。 XAML代码: UserControl的ViewModel: public ICommand ShowGuidCommand { 得到 { 返回新的ControlCommand(this.Sh
- XAML代码:
- UserControl的ViewModel:
public ICommand ShowGuidCommand
{
得到
{
返回新的ControlCommand(this.ShowGuid,null);
}
}
public void ShowGuid(对象变量)
{
string errMsg=string.Empty;
尝试
{
如果(!this.DataLayerLocal.CheckManagerRunning())
{
返回;
}
this.dispatcher.BeginInvoke(新操作(()=>
this.LoadShowGuidData(this.ShowGuidIsChecked));
}
捕获(自定义异常cex)
{
如果(!string.IsNullOrEmpty(errMsg))
{
DisplayMessageBox(errMsg,Properties.Resources.ControlsCaption);
}
}
}
公共无效加载ShowGuidData(布尔复选框状态)
{
Mouse.OverrideCursor=游标。等待;
string errMsg=string.Empty;
尝试
{
this.varValues=this.DataLayerLocal.GetVariablesInformation(this.ProcedurePath,
checkBoxStatus?1:0,out errMsg);
这个.NewLineRemover();
this.VariableList=this.varValues;
this.Sort();
}
捕获(自定义异常cex)
{
如果(!string.IsNullOrEmpty(errMsg))
{
DisplayMessageBox(errMsg,Properties.Resources.ControlsCaption);
}
}
最后
{
Mouse.OverrideCursor=null;
}
}
您可能应该考虑异步方法,这样您的事件可以继续进行,同时加载数据。@AnantJain:为什么要调用BeginInvoke
?该命令在UI线程上调用。您应该使用Task.Run
在后台线程上执行LoadShowGuidData
。这就是我想知道如何在此场景中使用Task的原因。我使用BeginInvoke只是为了表明它没有如问题中所述工作。
<DockPanel Margin="2">
<CheckBox Content="{x:Static p:Resources.checkBoxShowGUID}" Margin="2,0"
IsChecked="{Binding ShowGuidIsChecked,IsAsync=True,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Command="{Binding ShowGuidCommand}" HorizontalAlignment="Right" VerticalContentAlignment="Center"/>
</DockPanel>
public ICommand ShowGuidCommand
{
get
{
return new ControlCommand<object>(this.ShowGuid, null);
}
}
public void ShowGuid(object variable)
{
string errMsg = string.Empty;
try
{
if (!this.DataLayerLocal.CheckManagerRunning())
{
return;
}
this.dispatcher.BeginInvoke(new Action(() =>
this.LoadShowGuidData(this.ShowGuidIsChecked)));
}
catch (CustomException cex)
{
if (!string.IsNullOrEmpty(errMsg))
{
DisplayMessageBox(errMsg, Properties.Resources.ControlsCaption);
}
}
}
public void LoadShowGuidData(bool checkBoxStatus)
{
Mouse.OverrideCursor = Cursors.Wait;
string errMsg = string.Empty;
try
{
this.varValues = this.DataLayerLocal.GetVariablesInformation(this.ProcedurePath,
checkBoxStatus ? 1 : 0, out errMsg);
this.NewLineRemover();
this.VariableList = this.varValues;
this.Sort();
}
catch (CustomException cex)
{
if (!string.IsNullOrEmpty(errMsg))
{
DisplayMessageBox(errMsg, Properties.Resources.ControlsCaption);
}
}
finally
{
Mouse.OverrideCursor = null;
}
}