1

听说我正在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;
        }

    }

XAML 代码

 <TextBlock x:Name="Name" Foreground="White" Text="{Binding LineOne}"/>

我该怎么做。提前致谢

4

1 回答 1

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>

在资源中添加 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>

在 C# 中,如果您想更改 中的文本TextBlock,请在设置新文本之前调用以下命令。

    StoryBoard1.Begin();

并在完成上述方法后

    private void DoubleAnimation_Completed_1(object sender, EventArgs e)
    {
        //Change the text here before beginning of storyboard2
        TextBlockName.Text = "ABCD";
        StoryBoard2.Begin();
    }
于 2013-07-09T06:15:00.210 回答