gpt4 book ai didi

c# - 适用于 Windows Mobile 的 Silverlight 中的 Storyboard.GetTarget

转载 作者:行者123 更新时间:2023-11-30 20:11:39 24 4
gpt4 key购买 nike

我的 WP7 应用程序有问题。我正在尝试编写 WP7 应用程序表单 WPF 示例代码。

    private void storyboard_Completed(object sender, EventArgs e)
{
ClockGroup clockGroup = (ClockGroup)sender;

// Get the first animation in the storyboard, and use it to find the
// bomb that's being animated.
DoubleAnimation completedAnimation = (DoubleAnimation)clockGroup.Children[0].Timeline;
Bomb completedBomb = (Bomb)Storyboard.GetTarget(completedAnimation);

似乎没有 ClockGroup 类,Storyboard 也没有 GetTarget 方法(这有点奇怪,因为有 SetTarget 方法)。是否有 hack 来获得相同的功能?

最佳答案

我对 WPF 知之甚少,但在 Silverlight 或 WP7 中,Storyboard 的子项属于 TimeLine 类型。此外,StoryBoard 本身会有一个 Completed 事件,您可以将其绑定(bind)到该事件。所以至少第一段代码看起来像:-

private void storyboard_Completed(object sender, EventArgs e)
{
Storyboard sb = (Storyboard)sender;

DoubleAnimation completedAnimation = (DoubleAnimation)sb.Children[0];

现在是棘手的部分。

在 Silverlight 代码中使用 Storyboard.SetTarget 实际上很不寻常。我猜游戏代码更有可能在代码中生成元素和动画,因此更有可能使用SetTarget。如果这是您想要执行的操作,那么您将需要构建自己的附加属性,该属性同时具有 Get 和 Set,让此属性上的更改回调调用 Storyboard.SetTarget

这是代码:-

public static class StoryboardServices
{
public static DependencyObject GetTarget(Timeline timeline)
{
if (timeline == null)
throw new ArgumentNullException("timeline");

return timeline.GetValue(TargetProperty) as DependencyObject;
}

public static void SetTarget(Timeline timeline, DependencyObject value)
{
if (timeline == null)
throw new ArgumentNullException("timeline");

timeline.SetValue(TargetProperty, value);
}

public static readonly DependencyProperty TargetProperty =
DependencyProperty.RegisterAttached(
"Target",
typeof(DependencyObject),
typeof(Timeline),
new PropertyMetadata(null, OnTargetPropertyChanged));

private static void OnTargetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Storyboard.SetTarget(d as Timeline, e.NewValue as DependencyObject);
}
}

现在 SetTarget 代码将变为:-

 StoryboardServices.SetTarget(completedAnimation, bomb);

然后您完成的事件可以通过以下方式检索目标:-

 Bomb completedBomb = (Bomb)StoryboardServices.GetTarget(completedAnimation);

关于c# - 适用于 Windows Mobile 的 Silverlight 中的 Storyboard.GetTarget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3485272/

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