作为资源的WPF故事板

本文关键字:故事 WPF 资源 | 更新日期: 2023-09-27 18:08:40

我试图淡出然后淡出标签,它似乎从我的代码的某些部分工作,但不是从其他…

对于xaml,我有:

<Page x:Class=""Gtec2.MindBeagle.ChoosePatient"   .. .bla bla bla>
    <Page.Resources>
        <Resources Dictionary>
            <Storyboard x:Key="fadeInStory" Storyboard.TargetName="noPatientsLabel" Storyboard.TargetProperty="Opacity">
                <DoubleAnimation From="1" To="0" Duration="0:0:0.300"/>
            </Storyboard>
            <!-- Other resources as imagesources, styles and stuff -->
        </Resources Dictionary>
     </Page.Resources>
     <Grid>
         <!-- A lot of things -->
         <!-- And the guy I want to fadeIn and Out-->
         <TextBlock Name="noPatientsLabel" TextAlignment="Center" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" IsHitTestVisible="False">
        No Patients found <LineBreak/>
        please check the filters
         </TextBlock>
         <!-- A lot of things -->
     </Grid>
</Page>

后面的代码(c#)我有很多东西和一个像这样的方法:

public void FadeIn()
{
    Storyboard sb = FindResource("fadeInStory") as Storyboard;
    sb.Begin();
}

它似乎从相同的cs文件中工作,但当其他操作调用此方法以使标签出现时,它会抱怨说' 'noPatientsLabel'名称无法在'Gtec2.MindBeagle.ChoosePatient'. ';

想法吗?

作为资源的WPF故事板

我最后创建了一个静态类AnimationHelper,其中包含一些有用的函数。这里有一个例子

public static void FadeOut(UIElement target, int milliseconds)
    {
       DoubleAnimation da = new DoubleAnimation();
       da.From = target.Opacity;
       da.To = 0.0;
       da.Duration = TimeSpan.FromMilliseconds(milliseconds);
       da.AutoReverse = false;
       System.Windows.Media.Animation.Storyboard.SetTargetProperty(da, new PropertyPath("Opacity"));
       System.Windows.Media.Animation.Storyboard.SetTarget(da, target);
       System.Windows.Media.Animation.Storyboard sb = new System.Windows.Media.Animation.Storyboard();
       sb.Children.Add(da);
       sb.Begin();
   }

与淡入或其他相同。你也可以返回storyboard (public static System.Windows.Media.Animation.Storyboard FadeIn (UIElement target) { ... } .

)

如果你这样做,你可以附加到完成的事件:)

非常有用