2

我有一个 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 故事板时会出现以下错误:

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

我怎样才能解决这个问题?

4

1 回答 1

4

问题是 TranslateZoomRotateBehavior 正在用 MatrixTransform 替换您的 ScaleTransform,导致 Storyboard 中的前两个动画以不再存在的属性为目标。

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

于 2010-06-15T07:06:27.357 回答