WPF从c#代码后面访问资源

时间:2015-08-03 10:36:47

标签: c# wpf xaml

这是我的XAML:

   <TabControl Name="me" SelectionChanged="me_SelectionChanged">

        <TabControl.Resources>

            <Storyboard x:Name="fadein" x:Key="fatal">

                <DoubleAnimation x:Name="fade" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:3">

                </DoubleAnimation>
            </Storyboard>
        </TabControl.Resources>
        <TabItem Header="MyTabItem">
            <Grid Background="Black" HorizontalAlignment="Stretch" Opacity="0.7"></Grid>


        </TabItem>

    </TabControl>

我试图通过C#代码访问fatal动画,但我无法...

    public void me_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (sender == null)
            return;

        fatal.Stop();// cant recognize `fatal`
    }

C#无法识别fatal是什么。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

StoryBoard sb = (StoryBoard)me.FindResource("fatal");

StoryBoard sb = (StoryBoard)me.Resources["fatal"];