mscorlib.dll中发生了未处理的“System.NullReferenceException”类型异常

时间:2018-01-26 06:19:16

标签: c# wpf exception

Screenshot of my error here

我的代码总是收到System.NullReferenceException

中出现的类型为“mscorlib.dll”的未处理异常

附加信息:未将对象引用设置为对象的实例。

我正在使用Visual Studioperforming async等待磁贴的鼠标按下事件。现在我想从父窗口内的用户控件显示消息异步。

我跟踪我的Metro窗口声明中的错误。希望你能帮我解决这个问题。

这是我的代码

        public async void tileLockUser_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (dtgUserManagement.SelectedItems.Count > 0)
        {
            if (Convert.ToInt32(dtgUserManagement.SelectedValue) != 1)
            {
                MessageBox.Show(dtgUserManagement.SelectedValue.ToString());
                var confirm = new MetroDialogSettings()
                {
                    AffirmativeButtonText = "YES",
                    NegativeButtonText = "NO"
                };

                MetroWindow metroWindow = (Application.Current.MainWindow as MetroWindow);

                MessageDialogResult save = await metroWindow.ShowMessageAsync("CONFIRMATION", "DO YOU WANT TO LOCK USER?",
                                                                               MessageDialogStyle.AffirmativeAndNegative, confirm);

                if (save == MessageDialogResult.Affirmative)
                {
                    foreach (var row in dtgUserManagement.SelectedItems)
                    {
                        Membership user = db.Memberships.Find(dtgUserManagement.SelectedValue);
                        user.IsLockedOut = true;
                        user.LastLockedOutDate = DateTime.Now;
                    }
                    db.SaveChanges();
                    DataGridRefresh();
                    await metroWindow.ShowMessageAsync("USER", "Successfully locked!");
                }
            }
            else
            {
                //MessageBox.Show("User restricted to be lock!", "User Management");
                await metroWindow.ShowMessageAsync("WARNING", "User restricted to be lock!");
            }
        }
        else
        {
            //MessageBox.Show("Select use to lock", "User Management");
            await metroWindow.ShowMessageAsync("WARNING", "Select user to lock!");
        }
    }

1 个答案:

答案 0 :(得分:0)

this thread看起来如果你删除

MetroWindow metroWindow = (Application.Current.MainWindow as MetroWindow);

并使用metroWindow关键字替换this的所有出现,然后它应该有效。