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

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

问题描述

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



例如,我想用户点击一个按钮,都在UI做出改变,做一些工作,然后再进行一项改变......相反,用户。单击按钮和回调似乎在后台执行,然后将所有的UI在我眼前改变闪光灯



MainPage.xaml中:

 <电网X:NAME =LayoutRoot背景=白> 
< TextBlock的高度=23NAME =textBlock1文本=的TextBlockVerticalAlignment =顶部/>
<按钮点击=的button1_ClickCONTENT =按钮HEIGHT =23NAME =Button1的WIDTH =75/>
< /网格和GT;



MainPage.xaml.cs中:

 私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
textBlock1.Text =1;
//也试过了Thread.Sleep(5000);
Dispatcher.BeginInvoke(()=> Thread.sleep代码(5000));
textBlock1.Text =2;
}


解决方案

Silverlight使用工作队列项目来处理渲染和逻辑在UI线程上。由于你的逻辑也是在UI线程上运行(在 button_Click 处理),直到你的方法执行完毕,内部消息的渲染器不得到一个机会来绘制画面循环获取周围绘制屏幕。



的BeginInvoke 立即放置你的函数对Silverlight的工作队列到UI线程上执行(和立即返回) 。订单Silverlight的过程这可能是这样的:




  • 用户点击该按钮,将click事件的工作队列

  • Silverlight中看到了点击事件,并调用 button_Click (哪个地方的工作队列中的新动作)

  • 过程这可能是你的 Thread.sleep代码行动,暂停在UI线程5秒

  • 最后的Silverlight得到周围下一个工作队列项画屏幕



您想要做的是开始一个新的线程做你的工作,那么的BeginInvoke <什么/ code>的返回的到UI线程:

  VAR线程=新主题(( )= GT; {
Thread.sleep代码(5000);
Dispatcher.BeginInvoke(()=> {
//更新的UI
textBox.Text =2;
});
});

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



我要补充一点的Silverlight 5将组成线程可以执行某些操作不阻塞更新UI喜欢玩动画和GPU做UI线程加速渲染在可能的情况。


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

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:

<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:

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 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.

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:

  • 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

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();

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天全站免登陆