正在添加已加载的事件处理程序

本文关键字:事件处理 程序 加载 添加 | 更新日期: 2023-09-27 18:22:17

我想创建一个窗口,当它打开时,可以从屏幕底部向上滑动。然而,我的代码有问题,在将我的方法添加到Loaded事件时,我得到了以下错误

错误:

附加信息:值不能为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;
}

编辑:这是整个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();
            }
        }
    }

正在添加已加载的事件处理程序

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