1

我正在尝试实现一个简单的标签滚动器(在这里找到了这个想法:Scroller StackOverflow。我有一个标签,我想在 DoubleAnimation 类的帮助下对其进行动画处理,如下所示:

在构造函数中,我为 Loaded 实现事件:

    public Web()
    {
        InitializeComponent();
        SetDefaultBrowser();
        SetDefaultWebsite();
        //TextBlockSong.Text = GetSongName(GetBrowserName(), GetWebsiteName());
        Loaded += Window1_Loaded;

    }

事件:

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        DoubleAnimation doubleAnimation = new DoubleAnimation();
        doubleAnimation.From = -LabelNameSong.ActualWidth;
        doubleAnimation.To = canMain.ActualWidth;
        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
        doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
        LabelNameSong.BeginAnimation(Canvas.RightProperty, doubleAnimation);
    }

在我更新我的LabelNameSong内容之前,一切正常。LabelNameSong宽度与以前保持一致,并且我的动画从一开始就无法正常工作(使用更新的文本)。

我用 ListBox_SelectionChanged 事件更新了我的LabelNameSong

            private void ListBoxWebsite_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        double k = LabelNameSong.Width;
        double z = LabelNameSong.ActualWidth;
        LabelWebsiteName.Content = "Now on " + GetWebsiteName();
        LabelNameSong.Content = GetSongName(GetBrowserName(), GetWebsiteName());

        k = LabelNameSong.Width;
        z = LabelNameSong.ActualWidth;
    }

我用它们来测量宽度,发现它不会更新LabelNameSong的宽度。我是新来的,不知道该不该。

这是我的 xaml:

        <Canvas Background="White" Margin="0,182,58,0" >
        <Canvas ClipToBounds="True" Name="canMain" Height="80" Width="346"  >
            <Label FontSize="25" Foreground="#666666" Name="LabelNameSong" Canvas.Top="27" Height="30" Width="Auto" Content="This is very long text, I am testing it!" FontFamily="Calibri Light"/>
        </Canvas>
        </Canvas>

所以我的问题是,我可以做些什么来更新 LabelNameSong 的宽度以及我应该如何重新调用Window1_Loaded事件,以便 DoubleAnimation 的新实例可以与更新的 ActualWidth 一起使用?

        doubleAnimation.From = -LabelNameSong.ActualWidth;
        doubleAnimation.To = canMain.ActualWidth;

谢谢你。

4

1 回答 1

2

您可以将动画逻辑包装到一个方法中,然后从 Loaded 事件和您需要的任何其他事件中调用

private void CreateAnimation()
{
    DoubleAnimation doubleAnimation = new DoubleAnimation();
    doubleAnimation.From = -LabelNameSong.ActualWidth;
    doubleAnimation.To = canMain.ActualWidth;
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
    doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
    LabelNameSong.BeginAnimation(Canvas.RightProperty, doubleAnimation);
}

void Window1_Loaded(object sender, RoutedEventArgs e)
{
    CreateAnimation();
}

private void LabelNameSong_SizeChanged(object sender, RoutedEventArgs e)
{
    CreateAnimation();
}

但是您可能可以使用触发器在 Xaml 中完成这一切,并摆脱所有背后的代码。

在下面的示例中,动画将在加载时开始并在大小更改时重新启动

例子:

 <Label x:Name="LabelNameSong" Content="Hello" >
    <Label.Resources>
        <Storyboard x:Key="scroll">
            <DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10"
              Storyboard.TargetProperty="(Canvas.Right)"
              Storyboard.TargetName="LabelNameSong"
              RepeatBehavior="Forever"/>
        </Storyboard>
    </Label.Resources>

    <Label.Triggers>
        <EventTrigger RoutedEvent="Label.Loaded">
            <BeginStoryboard Storyboard="{StaticResource scroll}" />
        </EventTrigger>
        <EventTrigger RoutedEvent="Label.SizeChanged">
            <BeginStoryboard Storyboard="{StaticResource scroll}" />
        </EventTrigger>
    </Label.Triggers>
</Label>
于 2013-08-21T00:10:19.883 回答