我有一个 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”的不可变实例。
我怎样才能解决这个问题?