如何在Windows Phone 7中移动控件

时间:2014-04-09 17:44:19

标签: multithreading windows-phone-7

我有2个文本框,textbox1和textbox2 我想通过此代码从左到右移动textbox1和textbox2 我使用Thread.Sleep使它看起来像2个文本框正在移动,但它没有工作 请帮我解决一下,这是我的代码

double a = textBox1.Margin.Right;
double b = textBox2.Margin.Right;
double c = textBox1.Margin.Top;
double f = textBox2.Margin.Top;
textBox1.Text = textBox2.Margin.Left.ToString() + "," + textBox2.Margin.Top.ToString();
textBox2.Text = textBox1.Margin.Left.ToString() + "," + textBox1.Margin.Top.ToString();
while(c < f )
{
    textBox1.Margin = new Thickness(textBox1.Margin.Left, textBox1.Margin.Top, a+=10, textBox1.Margin.Bottom);
    textBox2.Margin = new Thickness(textBox2.Margin.Left, textBox2.Margin.Top, b+=10, textBox2.Margin.Bottom);
    c += 10;
    Thread.Sleep(20);
}

1 个答案:

答案 0 :(得分:0)

你应该在故事板中特别使用DoubleAnimation动画

示例:

<StackPanel>
    <StackPanel.Resources>
        <!-- Animates the rectangle's opacity. -->
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation
            Storyboard.TargetName="MyAnimatedRectangle"
            Storyboard.TargetProperty="Opacity"
            From="1.0" To="0.0" Duration="0:0:1"
            AutoReverse="True"/>
        </Storyboard>
    </StackPanel.Resources>
    <Rectangle MouseLeftButtonUp="Rectangle_Tapped"
        x:Name="MyAnimatedRectangle"
        Width="300" Height="200" Fill="Blue" />
</StackPanel>

http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206955(v=vs.105).aspx#BKMK_Animatingadoubleproperty

对于您的特定情况,我建议您为TranslateTransform.X和.Y属性设置动画,而不是在此处设置边距:

<StackPanel>
    <StackPanel.Resources>
        <!-- Animates the rectangle's opacity. -->
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation
            Storyboard.TargetName="rectangle1TranslateTransform"
            Storyboard.TargetProperty="X"
            From="0" To="100" Duration="0:0:1.50"
            AutoReverse="True"/>
        </Storyboard>
    </StackPanel.Resources>
    <TextBlock MouseLeftButtonUp="Rectangle_Tapped"
        Width="300" Height="200" Fill="Blue" >
        <TextBlock.RenderTransform>
             <TranslateTransform x:Name="rectangle1TranslateTransform"/>
        </TextBlock.RenderTransform>
    </TextBlock>
</StackPanel>