gpt4 book ai didi

wpf - WPF字幕文本动画

转载 作者:行者123 更新时间:2023-12-03 20:28:50 25 4
gpt4 key购买 nike

我可以使用TranslateTransform滚动文本,但是当动画快要完成时,我希望它重新开始。像蛇一样:)

这就是我得到的:

<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<StackPanel.RenderTransform>
<TranslateTransform x:Name="transferCurreny" X="-40"/>
</StackPanel.RenderTransform>
<StackPanel.Triggers>
<EventTrigger RoutedEvent="StackPanel.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0" To="-900" Duration="00:00:10"
Storyboard.TargetProperty="X"
Storyboard.TargetName="transferCurreny"
RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</StackPanel.Triggers>
<TextBlock FontSize="25" x:Name="txtKron" Margin="10,0,7,0"/>
</StackPanel>


这就是我想要的:

最佳答案

这样的事情应该可以解决问题。

您可以在Canvas中添加一个StackPanel,将2个TextBlocks设置为位置0,将一个ActualWidth设置为StackPanelCanvas,然后当第一个文本块离开屏幕时,另一个块将出现进入视野。

我使用Canvas的原因是因为ClipToBounds="false"是唯一实际支持TextBlock的元素,这即使第2个Canvas放置在IValueConverter本身的范围之外也可以显示

如果您想从右向左滚动,我们还需要一个SizeChanged以获得正确的负值。

我还在 上添加了事件触发器,因此,如果调整窗口大小,动画值将正确更新。

码:

namespace WpfApplication9
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();
}
}

public class NegatingConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double)
{
return -((double)value);
}
return value;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double)
{
return +(double)value;
}
return value;
}
}
}


Xaml:

<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication9"
Title="MainWindow" Height="83" Width="222" Name="UI" Tag="Tol Level">
<StackPanel Orientation="Horizontal" x:Name="stack">
<StackPanel.Resources>
<local:NegatingConverter x:Key="NegatingConverter" />
<Storyboard x:Key="slide">
<DoubleAnimation From="0" To="{Binding Width, ElementName=canvas, Converter={StaticResource NegatingConverter}}" Duration="00:00:10"
Storyboard.TargetProperty="X"
Storyboard.TargetName="transferCurreny"
RepeatBehavior="Forever"/>
</Storyboard>
</StackPanel.Resources>
<StackPanel.RenderTransform>
<TranslateTransform x:Name="transferCurreny" X="0"/>
</StackPanel.RenderTransform>
<StackPanel.Triggers>
<EventTrigger RoutedEvent="StackPanel.Loaded">
<BeginStoryboard Storyboard="{StaticResource slide}" />
</EventTrigger>
<EventTrigger RoutedEvent="StackPanel.SizeChanged">
<BeginStoryboard Storyboard="{StaticResource slide}" />
</EventTrigger>
</StackPanel.Triggers>
<Canvas x:Name="canvas" Width="{Binding ActualWidth, ElementName=stack}">
<TextBlock Text="StackOverflow" FontSize="25" x:Name="txtKron" Canvas.Left="0"/>
<TextBlock Text="{Binding Text, ElementName=txtKron}" FontSize="25" Canvas.Left="{Binding Width, ElementName=canvas}"/>
</Canvas>
</StackPanel>
</Window>


结果:



编辑:从左到右

 <StackPanel Orientation="Horizontal" x:Name="stack">
<StackPanel.Resources>
<local:NegatingConverter x:Key="NegatingConverter" />
<Storyboard x:Key="slide">
<DoubleAnimation From="0" To="{Binding Width, ElementName=canvas}" Duration="00:00:10"
Storyboard.TargetProperty="X"
Storyboard.TargetName="transferCurreny"
RepeatBehavior="Forever"/>
</Storyboard>
</StackPanel.Resources>
<StackPanel.RenderTransform>
<TranslateTransform x:Name="transferCurreny" X="0"/>
</StackPanel.RenderTransform>
<StackPanel.Triggers>
<EventTrigger RoutedEvent="StackPanel.Loaded">
<BeginStoryboard Storyboard="{StaticResource slide}" />
</EventTrigger>
<EventTrigger RoutedEvent="StackPanel.SizeChanged">
<BeginStoryboard Storyboard="{StaticResource slide}" />
</EventTrigger>
</StackPanel.Triggers>
<Canvas x:Name="canvas" Width="{Binding ActualWidth, ElementName=stack}">
<TextBlock Text="StackOverflow" FontSize="25" x:Name="txtKron" Canvas.Left="0"/>
<TextBlock Text="{Binding Text, ElementName=txtKron}" FontSize="25" Canvas.Left="{Binding Width, ElementName=canvas, Converter={StaticResource NegatingConverter}}"/>
</Canvas>
</StackPanel>

关于wpf - WPF字幕文本动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15323163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com