Silverlight 中的多个异步 UI 更新 [英] Multiple asynchronous UI updates in Silverlight

查看:16
本文介绍了Silverlight 中的多个异步 UI 更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Silverlight 回调执行多个 UI 更新?

How does one execute several UI updates from a Silverlight callback?

例如,我希望用户单击一个按钮,让 UI 进行更改,做一些工作,然后进行另一次更改...相反,用户单击按钮并且回调似乎在后台执行然后所有的 UI 变化都在我眼前一闪而过.

For example, I would like the user to click a button, have the UI make a change, do some work, then make another change... instead, the user clicks the button and the callback seems to execute in the background and then all of the UI changes flash before my eyes.

MainPage.xaml:

MainPage.xaml:

<Grid x:Name="LayoutRoot" Background="White" >
<TextBlock Height="23" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top"/>
<Button Click="button1_Click" Content="Button" Height="23" Name="button1" Width="75"  />
</Grid>

MainPage.xaml.cs:

MainPage.xaml.cs:

private void button1_Click(object sender, RoutedEventArgs e)
{
    textBlock1.Text = "1";
    // also tried Thread.Sleep(5000);
    Dispatcher.BeginInvoke(() => Thread.Sleep(5000));
    textBlock1.Text = "2";
}

推荐答案

Silverlight 使用工作项队列来处理 UI 线程上的呈现和逻辑.由于您的逻辑也在 UI 线程上运行(在您的 button_Click 处理程序中),渲染器没有机会绘制屏幕,​​直到您的方法完成执行并且内部消息循环开始绘制屏幕.

Silverlight uses a queue of work items to handle rendering and logic on the UI thread. Since your logic also runs on the UI thread (in your button_Click handler) the renderer doesn't get a chance to draw the screen until your method is done executing AND the internal message loop gets around to drawing the screen.

您的 BeginInvoke 立即将您的函数放在 Silverlight 工作队列中以在 UI 线程上执行(并立即返回).Silverlight 处理的顺序可能类似于:

Your BeginInvoke immediately places your function on the Silverlight work queue to execute on the UI thread (and returns immediately). The order Silverlight processes this is probably something like:

  • 用户点击按钮,在工作队列中放置一个点击事件
  • Silverlight 看到点击事件并调用 button_Click(在工作队列中放置一个新操作)
  • 处理下一个工作队列项,这可能是您的 Thread.Sleep 操作,暂停 UI 线程 5 秒
  • Silverlight 终于开始绘制屏幕
  • User clicks the button, placing a click event on the work queue
  • Silverlight sees the click event and calls button_Click (which places a new action on the work queue)
  • Process the next work queue item which is probably your Thread.Sleep action, pausing the UI thread for 5 seconds
  • Finally Silverlight gets around to painting the screen

你想要做的是启动一个新线程来完成你的工作,然后BeginInvoke 返回到UI线程:

What you want to do is start a new thread to do your work, then BeginInvoke back to the UI thread:

var thread = new Thread(() => {
  Thread.Sleep(5000);
  Dispatcher.BeginInvoke(() => { 
    // update UI 
    textBox.Text = "2";
  });
});

textBox.Text = "1";
thread.Start();

我应该补充一点,Silverlight 5 添加了一个合成线程,该线程可以执行某些操作来更新 UI,而不会阻塞 UI 线程,例如播放动画和在可能的情况下执行 GPU 加速渲染.

I should add that Silverlight 5 adds a composition thread that can perform certain actions to update the UI without blocking the UI thread like playing animations and doing GPU accelerated rendering where possible.

这篇关于Silverlight 中的多个异步 UI 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆