我的代码总是收到System.NullReferenceException
mscorlib.dll
”的未处理异常
附加信息:未将对象引用设置为对象的实例。
我正在使用Visual Studio
和performing 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!");
}
}
答案 0 :(得分:0)
从this thread看起来如果你删除
MetroWindow metroWindow = (Application.Current.MainWindow as MetroWindow);
并使用metroWindow
关键字替换this
的所有出现,然后它应该有效。