2015-11-16 59 views
0

我想打開一個窗口,從屏幕底部向上滑動。但是即時通訊與我的代碼的問題,加入我的方法Loaded事件時,即時通訊收到以下錯誤添加加載的事件處理程序

錯誤:

Additional information: Value cannot be null.

這是添加方法的事件處理程序的代碼,該方法:

//Position the Notification 
      var workingArea = SystemParameters.WorkArea; 
      this.Left = (workingArea.Width - this.ActualWidth)/2; 

      //Create storyboard for animation 
      Loaded += animate; 
     } 
    }    
} 

public RoutedEventHandler animate(object sender, RoutedEventArgs e) 
{ 
    var workingArea = SystemParameters.WorkArea; 

    Storyboard sb = new Storyboard(); 
    var slide = new DoubleAnimation() 
    { 
     BeginTime = TimeSpan.FromSeconds(2), 
     Duration = TimeSpan.FromSeconds(1), 
     By = -100 
    }; 
    Storyboard.SetTarget(slide,this); 
    Storyboard.SetTargetName(this, "MyWindow"); 
    Storyboard.SetTargetProperty(slide,new PropertyPath("(Window.Top)")); 

    this.Top = workingArea.Height - this.ActualHeight; 
    return null; 
} 

編輯: 這是整個窗口的代碼背後,應處理好動畫和定位。

/// <summary> 
    /// Interaction logic for NotificationAll.xaml 
    /// </summary> 
    public partial class NotificationAll : Window 
    { 
     public NotificationAll() : base() 
     { 
      InitializeComponent(); 
     } 
     public new void Show() 
     { 
      //Ensure new notifications are placed above older ones 
      if (!Application.Current.Windows.Cast<Window>().Where(x => 
      x != this).Any(x => x.GetType().Name == "NotificationAll")) 
      { 
       this.Topmost = true; 
       base.Show(); 

       this.Owner = System.Windows.Application.Current.MainWindow; 

       //Position the Notification 
       var workingArea = SystemParameters.WorkArea; 
       this.Left = (workingArea.Width - this.ActualWidth)/2; 

       //Create storyboard for animation 
       Loaded += SlideFromBottom; 
      } 
     } 

     public void SlideFromBottom(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("h"); 
      var workingArea = SystemParameters.WorkArea; 

      Storyboard sb = new Storyboard(); 
      var slide = new DoubleAnimation() 
      { 
       BeginTime = TimeSpan.FromSeconds(2), 
       Duration = TimeSpan.FromSeconds(1), 
       By = -100 
      }; 
      Storyboard.SetTarget(slide,this); 
      Storyboard.SetTargetName(this, "MyWindow"); 
      Storyboard.SetTargetProperty(slide,new PropertyPath("(Window.Top)")); 

      this.Top = workingArea.Height - this.ActualHeight; 
     } 

     /// <summary> 
     /// Close window once animation is complete 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void DoubleAnimationCompleted(object sender, EventArgs e) 
     { 
      if (!this.IsMouseOver) 
      { 
       this.Close(); 
      } 
     } 
    } 
+0

調試器必須將它指向一條線。那條線是什麼? –

+0

@EmpereurAiman已更新文章以來,現在有不同的錯誤 –

回答

1

好吧,這很簡單。您正在調用您的animate方法並將其結果分配給Loaded事件。在你的情況下,animate方法總是返回null。如果你想animate是一個事件處理程序,你不應該使用圓括號來調用它。你應該做Loaded += animate;
它應該有正確的簽名:void animate(object sender, RoutedEventArgs e)

+0

它應該有正確的簽名'無效的動畫(對象發件人,RoutedEventArgs e)' – Clemens

+0

是的,忘了它:) –

+1

或使用lambda表達式:'Loaded + = o,e)=> animate();' – Clemens

相關問題