2013-04-11 88 views
1

我真的是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

回答

0

我終於創建了一個名爲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有用:)