Blend Interaction Behavior提供“指向不可变实例”错误

时间:2010-06-14 06:35:25

标签: c# storyboard expression-blend blend

我有一个UserControl,它是其他用户控件的基类,以“模态视图”显示。
我希望所有用户控件在显示时淡入,在关闭时淡出。我还希望用户能够移动控件。我的构造函数看起来像这样:

var tg = new TransformGroup();
tg.Children.Add(new ScaleTransform());
RenderTransform = tg;
var behaviors = Interaction.GetBehaviors(this);
behaviors.Add(new TranslateZoomRotateBehavior());  

Loaded += ModalDialogBase_Loaded;

ModalDialogBase_Loaded方法如下所示:

private void ModalDialogBase_Loaded(object sender, RoutedEventArgs e)
{
    var fadeInStoryboard = (Storyboard)TryFindResource("modalDialogFadeIn");
    fadeInStoryboard.Begin(this);
}

当我按下控件上的“关闭”按钮时,会调用此方法:

protected virtual void Close()
{
    var fadeOutStoryboard = (Storyboard)TryFindResource("modalDialogFadeOut");  
    fadeOutStoryboard = fadeOutStoryboard.Clone();
    fadeOutStoryboard.Completed += delegate
    {
        RaiseEvent(new RoutedEventArgs(ClosedEvent));
    };
    fadeOutStoryboard.Begin(this);
}

淡出的故事板看起来像这样:

<Storyboard x:Key="modalDialogFadeOut">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="{x:Null}">
        <EasingDoubleKeyFrame KeyTime="0" Value="1">
            <EasingDoubleKeyFrame.EasingFunction>
                <BackEase EasingMode="EaseIn" Amplitude="0.3" />
            </EasingDoubleKeyFrame.EasingFunction>
        </EasingDoubleKeyFrame>
        <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0">
            <EasingDoubleKeyFrame.EasingFunction>
                <BackEase EasingMode="EaseIn" Amplitude="0.3" />
            </EasingDoubleKeyFrame.EasingFunction>
        </EasingDoubleKeyFrame>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="{x:Null}">
        <EasingDoubleKeyFrame KeyTime="0" Value="1">
            <EasingDoubleKeyFrame.EasingFunction>
                <BackEase EasingMode="EaseIn" Amplitude="0.3" />
            </EasingDoubleKeyFrame.EasingFunction>
        </EasingDoubleKeyFrame>
        <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0">
            <EasingDoubleKeyFrame.EasingFunction>
                <BackEase EasingMode="EaseIn" Amplitude="0.3" />
            </EasingDoubleKeyFrame.EasingFunction>
        </EasingDoubleKeyFrame>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="{x:Null}">
        <EasingDoubleKeyFrame KeyTime="0" Value="1" />
        <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0" />
        <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0" />
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

如果显示用户控件,并且用户没有在屏幕上移动它,一切正常。但是,如果用户移动控件,我会在启动modalDialogFadeOut故事板时收到以下错误:

路径中的'Children'属性值'(0)。(1)[0]。(2)'指向'System.Windows.Media.TransformCollection'的不可变实例。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

问题是TranslateZoomRotateBehavior正在用GraphTransform替换你的ScaleTransform,导致Storyboard中的前两个动画定位一个不再存在的属性。

由于您无法为Matrix的值设置动画以获得淡出效果,因此我将使用其他容器控件。行为的最外层,然后是内部的,以视觉方式淡出。