关注代码:
XAML:
<Window x:Class="Wpf_Notice.MostrarAviso"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf_Notice"
mc:Ignorable="d"
Title="Notice" Height="300" Width="300" WindowStyle="None" ShowInTaskbar="False" ResizeMode="NoResize" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<!--...-->
</StackPanel>
<Canvas Background="Black">
<Canvas Canvas.Bottom="0" ClipToBounds="True" Name="canMain" Background="Red" Height="97" Width="300">
<TextBlock FontSize="70" Name="tbmarquee" Height="74" FontFamily="Arial Black" Foreground="White" Canvas.Top="10"></TextBlock>
</Canvas>
</Canvas>
</Grid>
</Window>
快速动画:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
canMain.Width = ActualWidth;
tbmarquee.Text = "Donald Trump announced on Friday a new package of measures against North Korea bla bla bla bla bla bla bla bla...";
UpdateLayout();
DoubleAnimation doubleAnimation = new DoubleAnimation
{
From = -tbmarquee.ActualWidth,
To = canMain.ActualWidth,
RepeatBehavior = new RepeatBehavior(2),
Duration = new Duration(TimeSpan.FromSeconds(10))
};
tbmarquee.BeginAnimation(Canvas.RightProperty, doubleAnimation);
}
结果:(快速动画)
慢动画:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
canMain.Width = ActualWidth;
tbmarquee.Text = "Donald Trump announced...";
UpdateLayout();
DoubleAnimation doubleAnimation = new DoubleAnimation
{
From = -tbmarquee.ActualWidth,
To = canMain.ActualWidth,
RepeatBehavior = new RepeatBehavior(2),
Duration = new Duration(TimeSpan.FromSeconds(10))
};
tbmarquee.BeginAnimation(Canvas.RightProperty, doubleAnimation);
}
结果:(慢动画)
当文字很小时,动画很慢,现在当文字很长时,动画很快。如何以相同的速度保留2个文本?
任何解决方案?
答案 0 :(得分:1)
您可以尝试根据UI元素大小设置公式:
Duration = new Duration(TimeSpan.FromSeconds((tbmarquee.ActualWidth + canMain.ActualWidth) * 0.005)));
注意0.005
只是一个随机常数,调整它以增加或减少速度。因为字符具有不同的显示大小,所以您应该依赖于UI元素的大小,而不是依赖于string
的长度。