我有一个Web服务调用,并且想在Web服务收到错误时更新UI busyIndicator状态!
这是viewmodel webservice调用完成方法中的代码:
if (e.Error != null)
{
MessageBox.Show(msg);
busyIndicator.IsBusy = false;
return;
}
我知道当有多个线程时如何在另一个线程中更新UI对象,但是viewmodel没有对busyIndicator的引用!
最佳答案
对于MVVM模式,请执行以下操作
XAML文件
<controlsToolkit:BusyIndicator BusyContent="Fetching Data Please Wait.." IsBusy="{Binding IsBusy}" >
<Grid >....</Grid>
</controlsToolkit:BusyIndicator>
模型模型类
private bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
internal set { isBusy = value; OnPropertyChanged("IsBusy"); }
不,您只需要设置将为您完成工作的属性(property)的值(value)
类似于 View 模型
IsBusy = true; //or false
您是否尝试过这样的事情,即使用Dispatcher更新UI
private void btnClick_Click(object sender, RoutedEventArgs e)
{
busyIndicator.IsBusy = true;
//busyIndicator.BusyContent = "Fetching Data...";
ThreadPool.QueueUserWorkItem((state) =>
{
Thread.Sleep(3 * 1000);
Dispatcher.BeginInvoke(() => busyIndicator.IsBusy = false);
});
}