0

我有以下应该工作的:

XAML

<Window.Resources>
    <Storyboard x:Name="FadeInStoryboard1">
        <FadeInThemeAnimation Storyboard.TargetName="Image1" />
    </Storyboard>
    <Storyboard x:Name="FadeInStoryboard2">
        <FadeInThemeAnimation Storyboard.TargetName="Image2" />
    </Storyboard>
    <Storyboard x:Name="FadeInStoryboard3">
        <FadeInThemeAnimation Storyboard.TargetName="Image3" />
    </Storyboard>
    <Storyboard x:Name="FadeInStoryboard4">
        <FadeInThemeAnimation Storyboard.TargetName="Image4" />
    </Storyboard>
    <Storyboard x:Name="FadeInStoryboard5">
        <FadeInThemeAnimation Storyboard.TargetName="Image5" />
    </Storyboard>
</Window.Resources>
<Image Source="../Assets/image.png" x:Name="Image1" />
<Image Source="../Assets/image.png" x:Name="Image2" />
<Image Source="../Assets/image.png" x:Name="Image3" />
<Image Source="../Assets/image.png" x:Name="Image4" />
<Image Source="../Assets/image.png" x:Name="Image5" />

C#

//fades in the plane image 
private void FadeIn(int Index)
{
    if (Index == 0)
        FadeInStoryboard1.Begin();
    else if (Index == 1)
        FadeInStoryboard2.Begin();
    else if (Index == 2)
        FadeInStoryboard3.Begin();
    else if (Index == 3)
        FadeInStoryboard4.Begin();
    else
        FadeInStoryboard5.Begin();
}

上面的代码有效——但必须有更好的方法。有没有办法 (A) 将 Storyboard 对象放入我可以通过索引访问的列表中,或者 (B) 将 Storyboard 对象附加到作为其目标的 Image 对象上?

4

1 回答 1

1

遍历资源并检查它们的名称是一种选择吗?

string name = "FadeInStoryboard" + index;
foreach (object resource in this.Resources)
{
    if (resource is System.Windows.Media.Animation.Storyboard)
    {
        System.Windows.Media.Animation.Storyboard storyboard = (System.Windows.Media.Animation.Storyboard)resource;
        if (storyboard.Name == name) storyboard.Begin();

    }
  }

如果您在 Keys 中有一个模式,那么您可能可以使用

this.Resources.FindByName("MyKey"+index)
于 2013-08-10T10:18:26.060 回答