Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类DoubleAnimation-动画速度非常快,文本较长_C#_Wpf - Fatal编程技术网

C# 类DoubleAnimation-动画速度非常快,文本较长

C# 类DoubleAnimation-动画速度非常快,文本较长,c#,wpf,C#,Wpf,以下代码: 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

以下代码:

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);
}
结果:(快速动画)

慢速动画:

<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);
}
结果:(慢动画)

当文本较小时,动画速度较慢,而当文本较长时,动画速度较快。如何让两个文本保持相同的速度


任何解决方案?

您可以根据UI元素的大小尝试使用公式:

Duration = new Duration(TimeSpan.FromSeconds((tbmarquee.ActualWidth + canMain.ActualWidth) * 0.005)));

0.005
只是一个随机常数,调整它以增加或降低速度。与其依赖于
字符串的长度,不如依赖于UI元素的大小,因为字符的显示大小不同。

不太好,比较短文本和长文本的速度是不同的。@MatheusMiranda您能提供完整的工作代码示例吗?所以我可以在完全相同的条件下运行它。我只需要xaml。@MatheusMiranda看一下更新的答案。希望这能帮到你。