C# 在Thread.Sleep()之前更新页面布局

C# 在Thread.Sleep()之前更新页面布局,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我的页面上有一个TextBlock,带有Text值null(“”)。单击按钮时,我想更改此TextBlock的文本值,暂停半秒钟,然后将TextBlock一次移动一个像素到某个点 我尝试使用Thread.Sleep(),但到目前为止,我遇到了一个问题。我单击按钮,UI线程暂停半秒钟,然后TextBlock突然出现并开始移动。我希望它在我单击按钮时立即显示 我知道Thread.Sleep()不起作用。我愿意使用任何有效的工具。尝试使用故事板,而不是编写代码。我认为它比手动方式更适合您。故事板和动画

我的页面上有一个
TextBlock
,带有
Text
null
(“”)。单击按钮时,我想更改此
TextBlock
的文本值,暂停半秒钟,然后将
TextBlock
一次移动一个像素到某个点

我尝试使用
Thread.Sleep()
,但到目前为止,我遇到了一个问题。我单击按钮,UI线程暂停半秒钟,然后
TextBlock
突然出现并开始移动。我希望它在我单击按钮时立即显示


我知道Thread.Sleep()不起作用。我愿意使用任何有效的工具。

尝试使用故事板,而不是编写代码。我认为它比手动方式更适合您。

故事板和动画是在屏幕上移动项目的首选机制。一方面,它们经过优化,可以与电话线程模型配合使用。另一方面,将UI线程置于睡眠状态是一个坏主意,因为您正在制作一个无响应的应用程序

下面是一个如何使用故事板移动texblock的快速示例

用户界面元素。

 <Grid
  x:Name="ContentPanel"
  Grid.Row="1"
  Margin="12,0,12,0">
  <TextBlock
    Margin='79,263,177,307'
    Name='textBlock1'
    Text='TextBlock'
    RenderTransformOrigin="0.5,0.5">
    <TextBlock.RenderTransform>
        <CompositeTransform />
    </TextBlock.RenderTransform>
  </TextBlock>
  <Button
    Content="Button"
    Height="80"
    Margin="116,0,188,144"
    VerticalAlignment="Bottom"
    Click='Button_Click' />
</Grid>
 <phone:PhoneApplicationPage.Resources>
<Storyboard
  x:Name="MoveTextBlockStoryboard">
  <DoubleAnimationUsingKeyFrames
    Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)"
    Storyboard.TargetName="textBlock1">
    <EasingDoubleKeyFrame
      KeyTime="0"
      Value="0" />
    <EasingDoubleKeyFrame
      KeyTime="0:0:1.1"
      Value="120" />
  </DoubleAnimationUsingKeyFrames>
  <DoubleAnimationUsingKeyFrames
    Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
    Storyboard.TargetName="textBlock1">
    <EasingDoubleKeyFrame
      KeyTime="0"
      Value="0" />
    <EasingDoubleKeyFrame
      KeyTime="0:0:1.1"
      Value="-105" />
  </DoubleAnimationUsingKeyFrames>
</Storyboard>
private void Button_Click(object sender, RoutedEventArgs e) {
  textBlock1.Text = "new text";
  MoveTextBlockStoryboard.Begin();

}

或者考虑在后台工作线程中暂停,然后调用UI线程来更新它。但是克里斯的方法更好。+1,你就是这样做的。在页面上手动移动元素(在UI线程上!)是个坏主意。我建议你不要这样做。它很有魅力。谢谢很抱歉,我的回复太晚了。