谁能解释为什么当我使用调试器逐步完成单元测试时,在查看对象或属性时会得到空引用。例如:
1 [TestMethod]
2 [Description("Test to confirm that upon initial class creation, the login view is loaded as the default content for the TaskRegion.")]
3 public void Shell_Initialisation_LoginViewIsLoadedByDefault()
4 {
5 Shell shell = new Shell();
6
7 TestPanel.Children.Add(shell);
8
9 Shell_ViewModel viewModel = shell.DataContext as Shell_ViewModel;
10
11 Assert.IsTrue(viewModel.TaskRegionContent is ContentControl);
12
13 EnqueueTestComplete();
14 }
[第 9 行] 当我将 viewModel 字段设置为 shell 视图的 DataContext 时,我得到一个“对象未设置为实例...”异常。我确定我的数据上下文是在我的 shell.xaml.cs 中设置的;整个文件:
1 using System.Windows;
2
3 namespace eg.WorkManager.UI.Shell
4 {
5 public partial class Shell
6 {
7
8 public Shell()
9 {
10 InitializeComponent();
11 this.Loaded += new RoutedEventHandler(Shell_Loaded);
12 }
13
14 void Shell_Loaded(object sender, RoutedEventArgs e)
15 {
16 this.DataContext = new Shell_ViewModel();
17 }
18 }
19 }
20
我知道我做错了什么,但谁能解释一下?
谢谢,马克