在 DataTrigger 内绑定故事板动画会导致 XamlParser 崩溃 [英] Binding a Storyboard Animation inside a DataTrigger crashes the XamlParser

查看:30
本文介绍了在 DataTrigger 内绑定故事板动画会导致 XamlParser 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序在每次发生特定事件时将椭圆动画化到一个新位置.出于测试目的,我制作了一个更改视图模型属性的按钮,该属性绑定到触发动画的数据触发器,但稍后我希望视图模型根据我尚未实现的其他事件触发它 - 这就是为什么我可以不要在视图中直接使用绑定到该按钮的事件触发器,我需要视图模型.因为椭圆应该在每次被触发时被动画移动到一个新位置,所以我需要将 DoubleAnimation 的 TO-Property 绑定到视图模型中的一个值.当我使用普通事件触发器时,这工作正常,但使用数据触发器会使具有该绑定的 XamlParser 崩溃.除了 XAMLParseException 之外,我没有收到任何特定错误,但是,如果我将绑定更改为固定值,例如 5,它会正常工作.

I want my application to animate an ellipse to a new position everytime a certain event happens. For testing purposes I made a button that changes a viewmodel property and this property is bound to a datatrigger that fires the animation, but later on I want the viewmodel to trigger it based on other events I haven't implemented yet - that's why I can't use the eventtrigger being bound to that button directly in the view, I need the viewmodel. Because the ellipse is supposed to be moved by the animation to a new position everytime it gets fired, I need to bind the TO-Property of the DoubleAnimation to a value in the viewmodel. This works fine when I use a normal eventtrigger, but using the datatrigger crashes the XamlParser with that binding. I don't get any specific error except for an XAMLParseException, however, if I change the binding to a fixed value, for example 5, it'll work fine.

这是xaml:

<Window x:Class="AnimationExample.MainWindow"
        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:AnimationExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>

    <Canvas Name="CanvasWrapper" Width="525" Height="350">
        <Button Content="Next Animation" Command="{Binding NextAnimation}"/>
            <Ellipse Fill="Red" Width="60" Height="60" Canvas.Left="60" Canvas.Top="60">
            <Ellipse.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CanAnimate}" Value="True" >
                            <DataTrigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <!-- Works fine without the binding -->
                                        <DoubleAnimation
                                            Storyboard.TargetProperty="(Canvas.Left)"
                                            Duration="0:0:10" To="{Binding NextPosX}" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Ellipse.Style>
        </Ellipse>
    </Canvas>
</Window>

我相信解析器无法找到正确的绑定路径,即使代码完成显示了它.有没有办法获得正确的绑定路径?

I believe that the parser is unable to find the correct path to the binding, even though the code completion shows it. Is there even a way I can get the correct path to the binding?

推荐答案

阅读 数据绑定和动画动画:基本上动画是冻结创建的(即它的所有属性都是只读的,因此不能修改),必须重新创建动画以反映更改的属性通知.

Read the section Data Binding and Animating Animations : basically animations are created frozen (i.e. all of its properties are read-only and hence can't be modified), the animation has to be recreated to reflect on property changed notifications.

看看这个 博客条目this SOF 问题.

Have a look at this blog entry and at this SOF question.

根据您的评论更新:

首先为什么 DataTrigger 和 StoryBoard 绑定不起作用?

答案由故事板概述 如下:

不能使用动态资源引用或数据绑定表达式设置情节提要或动画属性值.那是因为Style 中的所有内容都必须是线程安全的,并且计时系统必须冻结 Storyboard 对象以使它们线程安全.故事板如果它或其子时间线包含动态资源,则无法冻结引用或数据绑定表达式.有关更多信息冻结和其他可冻结功能,请参阅可冻结对象概览.

You can't use dynamic resource references or data binding expressions to set Storyboard or animation property values. That's because everything inside a Style must be thread-safe, and the timing system must Freeze Storyboard objects to make them thread-safe. A Storyboard cannot be frozen if it or its child timelines contain dynamic resource references or data binding expressions. For more information about freezing and other Freezable features, see the Freezable Objects Overview.

为什么绑定适用于 EventTrigger 而不适用于 DataTrigger?

再次来自同一来源:

由属性触发对象应用的动画在更多比 EventTrigger 动画或动画开始复杂的方式使用故事板方法.他们通过定义的动画切换"其他 Trigger 对象,但与 EventTrigger 和方法触发的动画.

Animations applied by property Trigger objects behave in a more complex fashion than EventTrigger animations or animations started using Storyboard methods. They "handoff" with animations defined by other Trigger objects, but compose with EventTrigger and method-triggered animations.

我将假设他们的意思是使用 EventTriggers 在事件发生时重新创建 StoryBoard,但 DataTriggers 不是这种情况 - 切换"似乎意味着共享故事板.我将创建一个 SO 问题来验证这是什么意思(此处创建的问题:Storyboard: EventTrigger vs DataTrigger).

I will assume that what they mean is that with EventTriggers the StoryBoard is recreated at the time of the event, but this is not the case with DataTriggers - "handoff" seems to imply Sharing of the StoryBoard. I will create a SO question to verify what is meant by this (question created here: Storyboard: EventTrigger vs DataTrigger ).

这篇关于在 DataTrigger 内绑定故事板动画会导致 XamlParser 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆