2015-11-16 54 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

相关问题