gpt4 book ai didi

c# - 控制模板资源中 Storyboard 的 Hooking Completed 事件的问题

转载 作者:行者123 更新时间:2023-11-30 17:16:16 25 4
gpt4 key购买 nike

我正在创建一个名为 ImageFader 的自定义控件。样式模板如下所示:

<ControlTemplate TargetType="{x:Type controls:ImageFader}">
<Grid>
<Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="{TemplateBinding Stretch}"
x:Name="PART_SourceImage" Opacity="1" />

<Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="{TemplateBinding Stretch}"
x:Name="PART_TargetImage" Opacity="0" />

</Grid>
<ControlTemplate.Resources>
<Storyboard x:Key="FadeImageStoryboard">
<!-- fade in the new image -->
<DoubleAnimation From="0" To="1"
Duration="{TemplateBinding FadeTime}"
Storyboard.TargetName="PART_TargetImage"
Storyboard.TargetProperty="Opacity" />

<!-- fade out the previous image -->
<DoubleAnimation From="1" To="0"
Duration="{TemplateBinding FadeTime}"
Storyboard.TargetName="PART_SourceImage"
Storyboard.TargetProperty="Opacity" />

</Storyboard>
</ControlTemplate.Resources>
</ControlTemplate>

在代码中,我有这个:

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

SourceImage = Template.FindName("PART_SourceImage", this) as Image;
TargetImage = Template.FindName("PART_TargetImage", this) as Image;
FadeImageStoryboard = Template.Resources["FadeImageStoryboard"] as Storyboard;

FadeImageStoryboard.Completed += new EventHandler(FadeImageStoryboard_Completed);
}

在完成的处理程序中,我尝试交换图像并重置它们的不透明度:

private void FadeImageStoryboard_Completed(object sender, EventArgs e)
{
SourceImage.Source = TargetImage.Source;
SourceImage.Opacity = 1;
TargetImage.Opacity = 0;
}

我使用这个在 DispatchTimer 中启动动画:

FadeImageStoryboard.Begin(this, this.Template, true);

一切正常,直到我将两个或多个 ImageFader 控件放在同一个窗口上。当我这样做时(使用 DispatchTimer 以不同的时间间隔启动动画) Storyboard完成事件为两个 ImageFaders 运行。

我相信我的控件正在从模板资源中获取 Storyboard作为静态引用。

如何避免这种情况?

最佳答案

一个解决方案可能是从代码创建 Storyboard,它不会变得太丑:)

只需为此更改您的代码:

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

SourceImage = Template.FindName("PART_SourceImage", this) as Image;
TargetImage = Template.FindName("PART_TargetImage", this) as Image;
FadeImageStoryboard = GetFadeImageStoryboard();

FadeImageStoryboard.Completed += new EventHandler(FadeImageStoryboard_Completed);
}

private Storyboard GetFadeImageStoryboard()
{
DoubleAnimation fadeNewImage = new DoubleAnimation { From = 0, To = 1, Duration = FadeTime };
Storyboard.SetTarget(fadeNewImage, SourceImage);
Storyboard.SetTargetProperty(fadeNewImage, new PropertyPath("Opacity"));

DoubleAnimation fadePreviousImage = new DoubleAnimation { From = 0, To = 1, Duration = FadeTime };
Storyboard.SetTarget(fadePreviousImage, TargetImage);
Storyboard.SetTargetProperty(fadePreviousImage, new PropertyPath("Opacity"));

Storyboard fadeImageStoryboard = new Storyboard();
fadeImageStoryboard.Children.Add(fadeNewImage);
fadeImageStoryboard.Children.Add(fadePreviousImage);

return fadeImageStoryboard;
}

而且每个控件都会有自己的 Storyboard。我不认为将这些 Storyboard 作为模板的一部分是个好主意,因为它们是此控件正确行为的重要组成部分,而且它们也没有太多需要更改的地方(所以几乎所有代码必须重复的模板)。

关于c# - 控制模板资源中 Storyboard 的 Hooking Completed 事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7336533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com