如何在Windows手机中为TextBlock设置动画?

Sat*_*tti 1 xaml windows-phone-7

听到我将一些ObservableCollection数据绑定到我的文本块有一些时间差我没有任何动画显示数据,但我需要动画来显示.这是我的代码:

    DispatcherTimer timer = new DispatcherTimer();
    public ObservableCollection<ItemViewModel> Items { get; private set; }

    public Slideshow()
    {
        InitializeComponent();
        this.Items = new ObservableCollection<ItemViewModel>();
        DataContext = App.ViewModel;
        this.Items = App.ViewModel.Items;       
    }

    private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {
        itemNumber = 0;
        Name.Text = this.Items[itemNumber].LineOne;
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(5);
        timer.Tick += new EventHandler(timer_Tick);
        itemNumber++;
        timer.Start();  
    }

    public void timer_Tick(object sender, EventArgs e)
    {
        if (this.Items.Count > 0)
        {
            itemNumber++;
            Name.Text = this.Items[itemNumber].LineOne;
            if (itemNumber == this.Items.Count)
                itemNumber = 0;
        }

    }
Run Code Online (Sandbox Code Playgroud)

XAML代码

 <TextBlock x:Name="Name" Foreground="White" Text="{Binding LineOne}"/>
Run Code Online (Sandbox Code Playgroud)

我该怎么做.提前致谢

Ami*_*ngh 5

您可以使用StoryBoard动画TextBlock文本.你想要使用什么样的动画取决于你.在这里,我正在演示文本的淡化动画.通过将其不透明度从0设置为1,反之亦然.

在XAML中,

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Name="TextBlockName" Text="Hello" FontSize="25"/>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

在参考资料中添加StoryBoard

    <phone:PhoneApplicationPage.Resources>
        <Storyboard x:Name="StoryBoard1">
            <DoubleAnimation Storyboard.TargetName="TextBlockName"
                             Storyboard.TargetProperty="Opacity"
                             From="1" To="0" Duration="0:0:1"
                             Completed="DoubleAnimation_Completed_1"/>
        </Storyboard>

        <Storyboard x:Name="StoryBoard2">
            <DoubleAnimation Storyboard.TargetName="TextBlockName"
                             Storyboard.TargetProperty="Opacity"
                             From="0" To="1" Duration="0:0:1"/>
        </Storyboard>
    </phone:PhoneApplicationPage.Resources>
Run Code Online (Sandbox Code Playgroud)

在C#中,如果要更改文本TextBlock,请在设置新文本之前调用以下内容.

    StoryBoard1.Begin();
Run Code Online (Sandbox Code Playgroud)

并在完成上述方法后

    private void DoubleAnimation_Completed_1(object sender, EventArgs e)
    {
        //Change the text here before beginning of storyboard2
        TextBlockName.Text = "ABCD";
        StoryBoard2.Begin();
    }
Run Code Online (Sandbox Code Playgroud)