【问题标题】:Get storyboard from app.xaml and set target programmatically从 app.xaml 获取故事板并以编程方式设置目标
【发布时间】:2015-08-02 22:59:38
【问题描述】:

我尝试从 app.xaml 获取动画(向上/向下移动标签,如消息弹出窗口)。不过,当我尝试获取故事板时

app.xaml:

<Storyboard x:Key="AnimatedMessage">
            <DoubleAnimationUsingKeyFrames 
                                Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)"
                                Duration="0:0:10"
                                AutoReverse="True">
                <LinearDoubleKeyFrame Value="100" KeyTime="0:0:2"/>
                <LinearDoubleKeyFrame Value="200" KeyTime="0:0:4"/>
                <LinearDoubleKeyFrame Value="300" KeyTime="0:0:6"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

我的后台:

Storyboard SBStartAnimation = (Storyboard)this.FindResource("AnimatedMessage");
Storyboard.SetTarget(SBStartAnimation, LBAnimateMessage);
SBStartAnimation.Begin(this);

但这怪我,我不能为storyboard-object设置属性,因为它是只读的。

更新:

我喜欢动画的标签:

&lt;Label Content="" Name="LBAnimateMessage" Visibility="Hidden" Width="600" Style="{StaticResource Style_BasicMessage}" Margin="10,303,235,38"/&gt;

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    主要问题是您试图设置情节提要的目标,而不是设置它的动画。试试Storyboard.SetTarget(SBStartAnimation.Children.First(), LBAnimateMessage);

    不要忘记LBAnimateMessage 必须属于PropertyPath 属性。

    编辑:更正,PropertyPath 用于 SetTargetProperty。

    【讨论】:

    • 嗨 Dracor,它不适用于 Storyboard.SetTarget(SBStartAnimation.Children.First(), LBAnimateMessage); 我如何确保我的标签 LBAnimateMessagePropertyPath 的一部分?
    • 在这种情况下,只需从代码构造一个新的故事板,就可以了。
    • 您也可以尝试将 Storyboard 从 app.xaml 移动到页面/窗口 xaml
    • 使用 app.xaml 我尝试创建一个全局情节提要......使用 window.xaml 它不再那么全局了。
    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多