我的 WPF 应用程序有一个问题,它基本上有 2 个窗口、一个登录窗口和一个仪表板窗口,当我加载这些窗口时,我会间歇性地收到空引用异常。一个典型的异常如下所示(dashbaord)
Application: BlitsMe.Agent.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at System.Collections.Generic.Dictionary`2[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Insert(System.__Canon, System.__Canon, Boolean)
at MS.Internal.AppModel.ResourceContainer.GetResourceManagerWrapper(System.Uri, System.String ByRef, Boolean ByRef)
at MS.Internal.AppModel.ResourceContainer.GetPartCore(System.Uri)
at System.IO.Packaging.Package.GetPart(System.Uri)
at System.Windows.Application.LoadComponent(System.Object, System.Uri)
at BlitsMe.Agent.UI.WPF.Dashboard..ctor(BlitsMe.Agent.BlitsMeClientAppContext)
at BlitsMe.Agent.BlitsMeClientAppContext.RunDashboard()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
这是一个晦涩的错误。在 loadcomponent 类的深处。
我在主 AppContext 类中使用 2 种方法启动仪表板,如下所示
internal void SetupAndRunDashboard()
{
if (DashboardUiThread == null)
{
DashboardUiThread = new Thread(RunDashboard) { Name = "dashboardUIThread" };
DashboardUiThread.SetApartmentState(ApartmentState.STA);
DashboardUiThread.Start();
}
}
private void RunDashboard()
{
UIDashBoard = new Dashboard(this);
Dispatcher.Run();
}
我的仪表板构造函数看起来像这样
public partial class Dashboard : Window
{
public Dashboard(BlitsMeClientAppContext appContext)
{
this.InitializeComponent();
......
}
}
我真的很感激这方面的帮助,因为我真的很难过,因为它在 Windows API 中很深,空 ref 被抛出。