2011-05-18 60 views
3

我試圖彈出一個用戶控件,然後淡出超過3秒。我試圖使用下面的代碼,但是我一直得到不正確的參數值的Popup.LoadedEvent以及Splash.LoadedEvent的分配。我究竟做錯了什麼?WP7 - 通過代碼淡出用戶控件,而不是XAML

Splash s = new Splash(); 
      DoubleAnimation fade = new DoubleAnimation() 
      { 
       Duration = new Duration(TimeSpan.FromMilliseconds(3000)), 
       From = 1.0, 
       To = 0.0, 
       RepeatBehavior = new RepeatBehavior(1) 
      }; 

      fade.Completed += new EventHandler(fade_Completed); 

      this.popup = new Popup(); 
      this.popup.Child = s; 

      EventTrigger et = new EventTrigger(); 
      et.RoutedEvent = Popup.LoadedEvent; 

      Storyboard sb = new Storyboard(); 
      sb.Children.Add(fade); 

      BeginStoryboard bs = new BeginStoryboard() { Storyboard = sb }; 

      et.Actions.Add(bs); 

      this.popup.Triggers.Add(et); 
      this.popup.IsOpen = true; 

我似乎也無法弄清楚在哪裏/如何設置目標屬性。

編輯:我能夠使用提供的鏈接@ Titan2782得到答案。我已將它發佈在下面的答案中。

+0

如果你找到一個答案你自己的問題,請提交作爲一個答案,而不是。 – 2011-05-18 20:09:13

+0

@ H.B。我沒有找到我自己的答案。我從@ Titan2782提供的鏈接中得到答案。我爲什麼要拿走他的信用? – 2011-05-18 20:59:28

+0

你當然不應該聲稱它是你自己的,它只是格式的問題。你仍然可以接受他/她的回答,而且你仍然可以注意到這是他/她的感謝,但問題是回答問題和答案。 (例如在[這個問題](http://stackoverflow.com/questions/5854059/in-the-built-in-wpf-datagrid-can-i-set-the-datasource-for-a-datagridtemplatecolu/) asker做到了。) – 2011-05-18 21:04:49

回答

1

我有VB中的一個按鈕的例子,不應該是很難翻譯成C#:

Dim Fade As New Animation.DoubleAnimation 
Fade.From = 0.5 
Fade.To = 1 
Fade.Duration = TimeSpan.FromSeconds(3) 

Animation.Storyboard.SetTarget(Fade, button) 
Animation.Storyboard.SetTargetProperty(Fade, New PropertyPath(Button.OpacityProperty)) 

Dim sb As New Animation.Storyboard 
sb.Children.Add(highlight) 

sb.Begin() 

我想這個作品也有彈出。

1

感謝@ Titan2782回答我能弄明白

Splash s = new Splash(); 
       DoubleAnimation fade = new DoubleAnimation() 
       { 
        Duration = new Duration(TimeSpan.FromMilliseconds(4000)), 
        From = 1.0, 
        To = 0.0, 
        RepeatBehavior = new RepeatBehavior(1) 

       }; 

       fade.Completed += new EventHandler(fade_Completed); 

       this.popup = new Popup(); 
       this.popup.Child = s; 

       Storyboard.SetTargetProperty(fade, new PropertyPath(UIElement.OpacityProperty)); 
       sb.Children.Add(fade); 
       Storyboard.SetTarget(sb, s);   

       this.popup.IsOpen = true; 

       sb.Begin();