我正在尝试实现一个简单的标签滚动器(在这里找到了这个想法: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;
谢谢你。