【问题标题】:An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll [duplicate]mscorlib.dll 中出现“System.NullReferenceException”类型的未处理异常 [重复]
【发布时间】:2018-07-05 12:15:20
【问题描述】:

Screenshot of my error here

我的代码总是在 mscorlib.dll 中发生“System.NullReferenceException”类型的未处理异常

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

我在图块的鼠标按下事件中使用 Visual Studioperforming async await。现在我想显示来自父窗口内的用户控件的异步消息。

我跟踪错误来自我对 Metro Window 的声明。希望你能帮我解决这个问题。

这是我的代码

        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!");
        }
    }

【问题讨论】:

  • 我会在您创建 MetroWindow 的行上添加一个断点,然后您可以将鼠标悬停在 Application.Current.MainWindow 的每个部分上以查看哪个部分为空。您还可以在调试时使用即时或局部窗口来检查变量值。
  • 谢谢。如果它有效,我会尝试更新你。
  • 我尝试添加断点。 Application.Current.Maindow 的每个部分都不为空,但当它成为 MetroWindow 时,它整体为空。
  • Application.Current.MainWindow 不是 MetroWindow 因此 metroWindow 变量设置为 null 并且您尝试调用 ShowMessageAsync 的下一行将失败。您可以尝试使用is 关键字,然后使用(MetroWindow)Application.Current.MainWindow 进行硬转换,但我怀疑这也会失败。尝试在其上调用GetType() 方法以查看Application.Current.MainWindow 的底层类型是什么,然后您可以研究如何从中转换(或构造)MetroWindow

标签: c# wpf exception


【解决方案1】:

this thread 看来,如果你删除了

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

并将所有出现的metroWindow 替换为this 关键字,然后它应该可以工作。

【讨论】:

  • “这个关键字”是不可能的,因为我正在使用用户控件。
  • 想将你的应用程序的真正缩减版添加到粘贴 bin 中,以便我查看。这是一个 WPF 应用程序吧?
  • 找到了。就像我的问题
猜你喜欢
  • 2015-12-17
  • 2014-03-09
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多