添加加载的事件处理程序

时间:2015-11-16 20:52:07

标签: c# wpf

我想打开一个窗口,从屏幕底部向上滑动。但是我的代码有问题,我在将我的方法添加到Loaded事件时出现以下错误

错误:

  

附加信息:值不能为空。

这是将Method添加到Eventhandler的代码,以及Method:

//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;
}

修改 这是整个Window Code Behind,它应该处理动画和定位。

 /// <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();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

嗯,这很简单。您正在调用animate方法并将其结果分配给Loaded事件。在您的情况下,animate方法始终返回null。如果您希望animate成为事件处理程序,则不应使用括号来调用它。你应该做Loaded += animate;
它应该有正确的签名:void animate(object sender, RoutedEventArgs e)