WPF故事板作为资源

WPF故事板作为资源

问题描述:

我真的是WPF上的动画初学者,我真的迷失在命名空间的东西..我试图褪色淡出标签,它似乎工作从我的代码的一些部分,但没有从其他..生病这里总结一下我的代码,并让我们看看你找到那里:)WPF故事板作为资源

所以,对于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'的名称范围中找到。

如果尝试其他方法..就像在代码中创建整个故事板一样。实际上是冷却器,因为我创建了一个函数来淡入输入/输出任何你发送他作为参数的组件。但没有任何工作..

任何线索?顺便说一句,关于这一切的任何好的手册?

提前致谢!

+0

当你想在文本块中褪色?当一个财产是一定的价值?这个答案可能会帮助你[WP7 - 在Application.Resources中定义使用Storyboard](http://stackoverflow.com/questions/4653499)它适用于WP7,但仍然适用于WPF – ywm 2013-04-11 09:44:39

我终于创建了一个名为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(); 
    } 

相同的淡入或任何。您也可以返回故事板(公共静态System.Windows.Media.Animation.Storyboard FadeIn (UIElement target) { ... }

如果你这样做,你可以连接到成品事件:)

Extremelly有用:)