wpf 扩展工具包 BusyIndi​​cator 绑定(bind)到属性

标签 wpf mvvm-light wpftoolkit

我有一个 BusyIndi​​cator,我将 IsBusy 绑定(bind)到我的 View 模型中的 Busy 属性。

<xctk:BusyIndicator IsBusy="{Binding Busy}" x:Name="busyBox" Grid.Row="2"
              HorizontalAlignment="Center"
            VerticalAlignment="Center" BusyContent="Contacting Server" >
    </xctk:BusyIndicator>

我在启动网络服务调用(异步)时将忙切换为真,并在回调中将其设置为假。

这第一次很好用,之后每次都不再显示繁忙指示符。我在回调中添加了一个 thread.sleep(只是在 x=case 第二次移动太快的情况下)。

我知道我的属性正在正确通知,因为其他绑定(bind)控件并按预期工作。 busyindicator 似乎只适合一次使用

(顺便说一句,我正在使用 mvvm light 工具包 v3)

查看模型代码

this.Busy = true; //This proverty is declared correctly with notifications etc
IPersonSearchService searcher = new PersonSearchService(); //class that does my      webservice, ad i pass it a callback method from my UI (see below)
searcher.FindByPersonDetails(ps, GetAllPeopleCallback);


private void GetAllPeopleCallback (PersonSearchResult result, Exception e)
    {
        this.Busy = false;
        ((Models.PersonSearch)this.Model).Persons = result.Persons; //bound to my grid
         CommandManager.InvalidateRequerySuggested();  //i need to do this to make a button who's canexecute command binding happen         
    }

这是命中webservice的类

class PersonSearchService : IPersonSearchService
{
    public void FindByPersonDetails(WSPersonSearch.PersonSearch ps, Action<PersonSearchResult, Exception> Callback)
    {
        BackgroundWorker worker = new BackgroundWorker();

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            WSPersonSearch.PersonSearch search = (WSPersonSearch.PersonSearch)args.Argument;
            PersonSearchWebServiceClient wc = new PersonSearchWebServiceClient();
            PersonSearchResult r = wc.FindByPersonDetails(ps);
            args.Result = r;
        };

        worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
        {
            PersonSearchResult result = (PersonSearchResult)args.Result;
            Callback(result, null);
        };

        worker.RunWorkerAsync();
    }
}

用户界面上的其他一切都表现良好。我的按钮正确激活/停用。我的网格得到很好的更新等等

最佳答案

我想我解决了它。 看起来(而且经常是这种情况)并且通过发布上面的示例代码(删除我所有的测试困惑)我解决了它。

好的模型可以工作,但是因为我正在与网络服务通话,并且在第一次调用之后我的网络服务调用非常快,之后它移动得太快以至于我看不到 bsy infdicator。

所以为了解决这个问题......我变得懒惰并增加了 sleep 。但我把 sleep 放在回调中。因此,由于回调在 ui 线程中被触发,它在错误的点阻塞。当它进入休眠状态时,繁忙指示器已经出现并消失了。

所以我将它移到了 DoWork 方法中(在 ui threqad 之外)并且忙碌指示器保持不变。

傻我。感谢各位大侠指教!

关于wpf 扩展工具包 BusyIndi​​cator 绑定(bind)到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610721/

相关文章:

WPF 路径 : How to Draw this in XAML?

c# - 如何对具有多个子层的根 ViewModel 进行单元测试?

inversion-of-control - SimpleIoc.Default.GetInstance 和 ServiceLocator.Current.GetInstance 之间有区别吗

wpf - 为什么我的 ComboBox SelectedItem 为空?

c# - 为什么 ViewModelLocator 成员不是静态的

wpf - 在 WPF 中使用 .NET 2.0(Windows 窗体)控件的限制?

WPF 内容转换

wpf - 更改缩放顺序 WPF

c# - Caliburn 绑定(bind)异常

wpf - Silverlight WPF VisualStateManager 与 MultiDataTrigger