如何在viewmodel方法中更新busyIndi​​cator状态?

man*_*ov5 2 c# silverlight mvvm

我有一个webservice调用,我想在webservice收到错误时更新UI busyIndi​​cator状态!这是viewmodel webservice调用完成方法中的代码:

if (e.Error != null)
                {
                    MessageBox.Show(msg);
                    busyIndicator.IsBusy = false;
                    return;
                }
Run Code Online (Sandbox Code Playgroud)

我知道如何在有多个线程时更新另一个线程中的UI对象,但是viewmodel没有对busyIndi​​cator的引用!

Pra*_*ana 5

对于MVVM模式,做这样的事情

XAML文件

  <controlsToolkit:BusyIndicator BusyContent="Fetching Data Please Wait.." IsBusy="{Binding IsBusy}" >
            <Grid >....</Grid>
        </controlsToolkit:BusyIndicator>
Run Code Online (Sandbox Code Playgroud)

VIew Model类

private bool isBusy = false; 

public bool IsBusy 

{ 

    get { return isBusy; } 

    internal set { isBusy = value; OnPropertyChanged("IsBusy"); } 
Run Code Online (Sandbox Code Playgroud)

不,你只需要设置能够为你工作的财产的价值

在视图模型中的东西

    IsBusy = true; //or false
Run Code Online (Sandbox Code Playgroud)

你试过这样的事情,即用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);
});
}
Run Code Online (Sandbox Code Playgroud)