需要对象引用,也将转储对象转储到消息框提示中

时间:2015-11-12 02:10:21

标签: c#

我在使用此代码时遇到错误new RoutedEventHandler(Notifications.showNotifications(window));说:

  

非静态字段,方法或者需要对象引用   property' Notifications.showNotifications(Window)'

另外,我是否可以获得一个示例,说明如何从_notifications类中的runtimeObject对象中转储出每个Dictionary对象?我需要在调用showNotifications时执行此操作!

当我说转出时,根据我尝试在代码中添加的测试通知,我只想显示一个MessageBox来显示它们正在被添加,所以使用MessageBox上面的代码会显示:

的MessageBox

Error Code: 1, Text: Error 1
Error Code: 2, Text: Normal Message
Error Code: 3, Text: Tip

App.xaml.cs

namespace Test_Project
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //Startup
            Window main = new MainWindow();
            main.Show();

            //Attach Event Handlers to MainWindow
            if (!attachEventHandlers(main))
            {
                MessageBox.Show("Fatal Error: Unable to attach event handlers, contact administrator!", 
                    "Fatal Error!",
                    MessageBoxButton.OK,
                    MessageBoxImage.Exclamation);
            }
        }
        public bool attachEventHandlers(Window window)
        {

            //window.MouseMove += new RoutedEventHandler(Notifications.showNotifications(window));
            return true;
        }
    }

    public class Notifications
    {
        /// <summary>
        /// Show the notifications
        /// </summary>
        /// <param name="window"></param>
        public void showNotifications(Window window)
        {
            // Find the resource, then cast it to a runtimeObject
            var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables");

            //Create messagebox with all the notifications in to test
        }

        public bool addNotifications()
        {
            // Find the resource, then cast it to a runtimeObject
            var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables");

            //Create Dictionary
            Dictionary<int, string> arr = new Dictionary<int, string>();
            arr.Add(1, "Error 1");
            arr.Add(2, "Normal Message");
            arr.Add(3, "Tip");

            //Create test notifications
            runtime.notifications.Add(arr);
            return true;
        }
    }

    /// <summary>
    /// Global values for use during application runtime
    /// </summary>
    public class runtimeObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }

        }

        //Can the application be closed?
        private bool _inProgress = false;
        public bool inProgress
        {
            get { return _inProgress; }
            set
            {
                if (_inProgress != value)
                {
                    _inProgress = value;
                    OnPropertyChanged("inProgress");
                }
            }
        }

        /// <summary>
        /// Notifications held
        /// Array((int)type,(string)"message")
        /// </summary>
        private List<Dictionary<int, string>> _notifications;
        public List<Dictionary<int, string>> notifications
        {
            get { return _notifications; }
            set
            {
                if (_notifications != value)
                {
                    _notifications = value;
                    OnPropertyChanged("notifications");
                }
            }
        }

        //Selected folder to search in
        private int _uploadProgress = 0;
        public int uploadProgress
        {
            get { return _uploadProgress; }
            set
            {
                if (_uploadProgress != value)
                {
                    //int Angle = (_uploadProgress * 360) / 100;
                    //Classes.CircularProgress.RenderArc(Angle);
                    _uploadProgress = value;
                    OnPropertyChanged("uploadProgress");
                }
            }
        }
    }
}

0 个答案:

没有答案