为什么在设置 IsBusy = true 后我的 Silverlight BusyIndi​​cator 保持不可见?

发布于 2024-10-14 08:32:16 字数 580 浏览 4 评论 0原文

当 DomainDataSource 获取数据时,我的 BusyIndi​​cator 按预期工作。但是当我在代码中将 IsBusy 设置为 true 时,该控件保持不可见。

我正在使用 Silverlight 4 和 BusyIndi​​cator 工具包。在 XAML 中,我已将 BusyIndi​​cator.IsBusy 属性绑定到 DomainDataSource 控件的 IsBusy 属性。 BusyIndi​​cator 控件包装了我的主 Grid (LayoutRoot) 和所有子控件。

<toolkit:BusyIndicator IsBusy="{Binding ElementName=labSampleDomainDataSource, Path=IsBusy}"  Name="busyIndicator1">
<Grid x:Name="LayoutRoot">

</Grid>
</toolkit:BusyIndicator>

问题是当我设置 busyIndi​​cator1 = true; 时 BusyIndi​​cator 不显示;在按钮单击事件中。知道我做错了什么吗?

My BusyIndicator works as expected when the DomainDataSource fetches data. But the control stays invisible when I set IsBusy to true in code.

I'm using Silverlight 4 and the toolkit BusyIndicator. In XAML I have bound the BusyIndicator.IsBusy property to the IsBusy property of my DomainDataSource control. The BusyIndicator control wraps my main Grid (LayoutRoot) and all child controls.

<toolkit:BusyIndicator IsBusy="{Binding ElementName=labSampleDomainDataSource, Path=IsBusy}"  Name="busyIndicator1">
<Grid x:Name="LayoutRoot">

</Grid>
</toolkit:BusyIndicator>

The problem is that the BusyIndicator does not show when I set busyIndicator1 = true; in a button click event. Any idea what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-10-21 08:32:16

UI 在 UI 线程上非常自然地更新自身。按钮单击等事件也在 UI 线程上运行。

在某些情况下,属性更改和对控件的方法调用将导致方法被分派到 UI 线程。这意味着要调用的方法在 UI 线程可用于执行该方法之前不会实际发生(并且已执行排队的其他任何内容)。 IsBusy 属于此类。

只有当您的代码完成 UI 线程时,UI 才有机会更新其外观。您不应该在 UI 线程中执行任何长时间运行的任务。在你的情况下,你绑定线程来执行你自己的命令,并让 UI 缺乏可用来完成工作的线程。

相反,您应该使用 BackgroundWorker 类,并在其在不同线程上运行的 DoWork 事件中完成所有繁重的工作。

myButton_Click(object sender, RoutedEventArgs e)
{
    isbusyindicator1.IsBusy = true;
    var bw = new BackgroundWorker();
    bw.DoWork += (s, args) =>
    {
       //Stuff that takes some time
    };
    bw.RunWorkerCompleted += (s, args) =>
    {
         isbusyindicator1.IsBusy = false;
    };
    bw.RunWorkerAsync();
}

现在,此单击事件完成得非常快,允许使用 UI 线程来更新 UI,而实际工作则在后台线程上完成。完成后,IsBusy 被清除。

The UI updates itself quite naturally on the UI Thread. Events for things like button clicks also run on the UI thread.

In some cases property changes and method calls into controls will result in a method being dispatched to the UI thread. What this means is that the method to be invoked will not actually occur until the UI thread becomes available to execute it (and anything else already queued up has been executed). IsBusy falls into this category.

Only when your code has finished with the UI thread will the UI get a chance to update its appeareance. You should not be doing any long running tasks in the UI thread. In your case you tie up the thread to do your own bidding and leave the UI starved of this one thread it can use to get its work done.

Instead you should look to use the BackgroundWorker class and do all the heavy work in its DoWork event which runs on a different thread.

myButton_Click(object sender, RoutedEventArgs e)
{
    isbusyindicator1.IsBusy = true;
    var bw = new BackgroundWorker();
    bw.DoWork += (s, args) =>
    {
       //Stuff that takes some time
    };
    bw.RunWorkerCompleted += (s, args) =>
    {
         isbusyindicator1.IsBusy = false;
    };
    bw.RunWorkerAsync();
}

Now this click event completes very quickly allowing the UI thread to be used to update the UI whilst the actual work is done on a background thread. When completed the IsBusy is cleared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文