C# 尝试创建UserControl实例时,获取NullPointerException

C# 尝试创建UserControl实例时,获取NullPointerException,c#,wpf,visual-studio-2010,visual-studio-2008,user-controls,C#,Wpf,Visual Studio 2010,Visual Studio 2008,User Controls,当我尝试使用同一解决方案中的UserControl时,我在InitializeComponent()行中得到NullPointerException 我的MainWindow.xaml.cs文件,我在其中添加UserControl: ViewStudentUc viewStudentUc = new ViewStudentUc(); viewStudentGrid.Children.Add(viewStudentUc); 我的ViewStudentUc.xaml文件中出现异常: public

当我尝试使用同一解决方案中的
UserControl
时,我在
InitializeComponent()
行中得到
NullPointerException

我的
MainWindow.xaml.cs
文件,我在其中添加
UserControl

ViewStudentUc viewStudentUc = new ViewStudentUc();
viewStudentGrid.Children.Add(viewStudentUc);
我的
ViewStudentUc.xaml
文件中出现异常:

public ViewStudentUc()
{
    InitializeComponent();   //Where I'm getting NullPointerException.
}
为什么会出现此异常,如何删除它

这是调用堆栈

SNS School Management.exe!SNS_School_Management.ViewStudentUc.ViewStudentUc()第25行C#
SNS学校管理.exe!SNS_School_Management.main window.viewStudent_按钮_单击(对象发送器,System.Windows.routedEventTargets e)第39行+0x15字节C#
[外部代码]

请显示完整的堆栈跟踪。您在.Designer.CS文件中有一个无效的对象。我知道你通常不会去编辑那个文件。。。但是这种情况对我来说经常发生。这被标记为wpf,所以没有designer.cs。创建新组件的代码使用哪种方法?你能调试吗?这是一个事件,当点击一个按钮时,我试图隐藏上一个网格并用这个用户控件替换它。@Nitin:听起来很奇怪-你能在某个地方发布完整的解决方案吗?(理想情况下,其中不包含任何非必需代码。)