标签冻结并且不改变它的Left属性

本文关键字:Left 属性 改变 冻结 标签 | 更新日期: 2023-09-27 18:10:00

基本上我试着用两个标签做一个选框,当一个不可见时,第二个开始它的动画,所以它从左到右平滑,文本总是可见的。我的问题是,它确实在开始时工作,但当我重试该代码(第二次在运行时,所以旧的动画停止,它刷新文本,并再次启动动画)第二个标签冻结时,它是,它的左属性的画布不改变(画布。SetLeft (L_Content_Second -L_Content_Second.ActualWidth)

<标题> c# h1> 是一个叫做StartAnimation()的函数;当L_Content和L_Content_Second的内容发生变化时触发
DoubleKeyFrameCollection collection = new DoubleKeyFrameCollection();
collection.Add(
new LinearDoubleKeyFrame(
    -L_Content.ActualWidth,
    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)))
);
if (L_Content.ActualWidth > this.ActualWidth)
{
    collection.Add(
    new LinearDoubleKeyFrame(
        this.ActualWidth - (L_Content.ActualWidth / 2),
        KeyTime.FromTimeSpan(TimeSpan.FromSeconds(15)))
    );
    collection.Add(
    new LinearDoubleKeyFrame(
        this.ActualWidth * 2,
        KeyTime.FromTimeSpan(TimeSpan.FromSeconds(30)))
    );
}
else
{
    collection.Add(
    new LinearDoubleKeyFrame(
        this.ActualWidth - L_Content.ActualWidth,
        KeyTime.FromTimeSpan(TimeSpan.FromSeconds(15)))
    );
    collection.Add(
    new LinearDoubleKeyFrame(
        this.ActualWidth + (this.ActualWidth - L_Content.ActualWidth),
        KeyTime.FromTimeSpan(TimeSpan.FromSeconds(30)))
    );
}
animK.KeyFrames = collection;
animK.Duration = TimeSpan.FromSeconds(30);
animK2.KeyFrames = collection;
animK2.BeginTime = TimeSpan.FromSeconds(15);
animK2.Duration = TimeSpan.FromSeconds(30);

animK.AutoReverse = false;
animK2.AutoReverse = false;
animK.FillBehavior = FillBehavior.Stop;
animK.RepeatBehavior = RepeatBehavior.Forever;
animK2.FillBehavior = FillBehavior.Stop;
animK2.RepeatBehavior = RepeatBehavior.Forever;
Canvas.SetLeft(L_Content_Second, -L_Content_Second.ActualWidth); //This works at start, then it doesn't any more
L_Content_Second.Visibility = System.Windows.Visibility.Visible;
L_Content.BeginAnimation(Canvas.LeftProperty, animK, HandoffBehavior.SnapshotAndReplace);
L_Content_Second.BeginAnimation(Canvas.LeftProperty, animK2, HandoffBehavior.SnapshotAndReplace);
<标题> XAML h1> div class="answers">

我试图通过XAML

下面是相同的示例,运行它,看看它是否符合您的要求。

<Canvas x:Name="Container">
    <Canvas.Resources>
        <l:PositionConverter xmlns:l="clr-namespace:CSharpWPF"
                                 x:Key="PositionConverter" />
        <Storyboard x:Key="marquee">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="L_Content"
                                           RepeatBehavior="Forever"
                                           Storyboard.TargetProperty="(Canvas.Left)"
                                           Duration="0:0:10">
                <SplineDoubleKeyFrame KeyTime="0:0:0"
                                      Value="{Binding ActualWidth, ElementName=L_Content, Converter={StaticResource PositionConverter}}" />
                <SplineDoubleKeyFrame KeyTime="0:0:5"
                                      Value="{Binding ActualWidth,ElementName=Container}" />
                <SplineDoubleKeyFrame KeyTime="0:0:10"
                                      Value="{Binding ActualWidth,ElementName=Container}" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="L_Content_Second"
                                           RepeatBehavior="Forever"
                                           Storyboard.TargetProperty="(Canvas.Left)"
                                           Duration="0:0:10">
                <SplineDoubleKeyFrame KeyTime="0:0:0"
                                      Value="{Binding ActualWidth, ElementName=L_Content_Second, Converter={StaticResource PositionConverter}}" />
                <SplineDoubleKeyFrame KeyTime="0:0:5"
                                      Value="{Binding ActualWidth, ElementName=L_Content_Second, Converter={StaticResource PositionConverter}}" />
                <SplineDoubleKeyFrame KeyTime="0:0:10"
                                      Value="{Binding ActualWidth,ElementName=Container}" />
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Canvas.Resources>
    <TextBox x:Name="L_Content"
             Foreground="Red"
             Text="first" />
    <TextBox x:Name="L_Content_Second"
             Foreground="Green"
             Text="second">
    </TextBox>
    <Button Content="Start" Canvas.Top="30">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard Storyboard="{StaticResource marquee}" />
            </EventTrigger>
        </Button.Triggers>
    </Button>
    <Canvas.Triggers>
        <EventTrigger SourceName="L_Content"
                      RoutedEvent="SizeChanged">
            <BeginStoryboard Storyboard="{StaticResource marquee}" />
        </EventTrigger>
        <EventTrigger SourceName="L_Content_Second"
                      RoutedEvent="SizeChanged">
            <BeginStoryboard Storyboard="{StaticResource marquee}" />
        </EventTrigger>
        <EventTrigger RoutedEvent="SizeChanged">
            <BeginStoryboard Storyboard="{StaticResource marquee}" />
        </EventTrigger>
    </Canvas.Triggers>
</Canvas>

我在这个例子中使用了文本框来轻松地改变值,也为此我创建了一个转换器

namespace CSharpWPF
{
    public class PositionConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return -System.Convert.ToDouble(value);
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

标签冻结并且不改变它的Left属性