WPF窗口在启动时抛出TypeInitializationException

我有一个带有几个色带的Excel AddIn,一个色带是设置窗口。 单击它时,将显示自定义窗口。 但是,单击时没有任何内容显示。 我在日志文件中看到以下exception。 是什么导致这种情况以及如何解决? 非常感谢

 2012-04-09 09:59:50,161 [1] ERROR Helper [(null)]  -  Name:TypeInitializationException消息:'System.Windows.Window'的类型初始值设定项引发exception。 目标:Void .ctor()Stack:位于MyAddIn.Connect.BtnClick(IRibbonControl控件)MyAddIn.Connect.GetSettings()的MyShared.View.ConnectionSetup..ctor()处的System.Windows.Window..ctor()处。 :TypeInitializationException消息:'System.Windows.FrameworkElement'的类型初始值设定项引发exception。 目标:Void .cctor()Stack:at System.Windows.Window..cctor()Name:TypeInitializationException消息:'System.Windows.Documents.TextElement'的类型初始值设定项引发exception。 目标:Void .cctor()Stack:at System.Windows.FrameworkElement..cctor()Name:TypeInitializationException消息:'MS.Internal.FontCache.Util'的类型初始值设定项引发exception。 目标:Int32 get_Dpi()Stack:在System.Windows.Dindows.TextElement..cctor()处的System.Windows.SystemFonts.ConvertFontHeight(Int32 height)处的MS.Internal.FontCache.Util.get_Dpi()处。名称:UriFormatException消息:无效的URI:无法确定URI的格式。 目标:Void CreateThis(System.String,Boolean,System.UriKind)Stack:位于MS的System.Uri..ctor(String uriString,UriKind uriKind)的System.Uri.CreateThis(String uri,Boolean dontEscape,UriKind uriKind)。 Internal.FontCache.Util..cctor() 

编辑,这是xaml代码

                                                <!---->                                            Save username and password                   

单击function区时,将执行代码,ConnectionSetup是上面的xaml文件

 private bool GetSettings() { var settingUI = new ConnectionSetup(); settingUI.DataContext = Settings; CenterMainWindow(settingUI, wndHandle.Handle); bool? result = settingUI.ShowDialog(); return result ?? false; } private void CenterMainWindow(System.Windows.Window window, IntPtr ownerHandle) { var helper = new System.Windows.Interop.WindowInteropHelper(window) {Owner = ownerHandle}; // Center window // Note - Need to use HwndSource to get handle to WPF owned window, // and the handle only exists when SourceInitialized has been // raised window.SourceInitialized += delegate { // Get WPF size and location for non-WPF owner window var nonWPFOwnerLeft = XLApp.Left; var nonWPFOwnerWidth = XLApp.Width; var nonWPFOwnerTop = XLApp.Top; var nonWPFOwnerHeight = XLApp.Height; // Get transform matrix to transform non-WPF owner window // size and location units into device-independent WPF // size and location units var source = System.Windows.Interop.HwndSource.FromHwnd(helper.Handle); if (source != null && source.CompositionTarget != null) { var matrix = source.CompositionTarget.TransformFromDevice; var ownerWPFSize = matrix.Transform(new System.Windows.Point(nonWPFOwnerWidth, nonWPFOwnerHeight)); var ownerWPFPosition = matrix.Transform(new System.Windows.Point(nonWPFOwnerLeft, nonWPFOwnerTop)); // Center WPF window window.WindowStartupLocation = WindowStartupLocation.Manual; window.Left = Math.Max(0, ownerWPFPosition.X + (ownerWPFSize.X - window.Width)/2); window.Top = Math.Max(0, ownerWPFPosition.Y + (ownerWPFSize.Y - window.Height)/2); } }; } 

从您发布的错误堆栈判断,您的设置窗口可能上面有一个链接。 不幸的是,链接中包含一个无效的URL,这导致窗口的创建失败。

编辑:仔细查看了您的代码和错误消息,很明显我正在寻找错误的区域。 一些搜索发现了wpf中的一个错误 。

短版本:windir环境变量可能在目标机器上设置不正确,导致字体子系统中断。 解决方法是在目标计算机的注册表或代码中修复环境变量,方法是在启动时添加以下内容:

 Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot")); 

我们有几个人遇到过这个问题。 此处描述的解决方案(由PATH大于2048个字符引起)解决了该问题。

我担心上面提出的声明可能会在运行过多或太少特权的代码中出现问题。 当然,我的应用程序在我的工作站上以管理员身份运行,对于调用Environment.SetEnvironmentVariable(“windir”,Environment.GetEnvironmentVariable(“SystemRoot”))没有问题;

但我不确定SetEnvironmentVariable实际上是否可以在锁定的操作系统上运行。 有没有人a)尝试这是真实的,b)在低特权环境中尝试过>

此外,我在使用此调用安装应用程序后,在应用程序启动时看到一些奇怪的情况,并运行一次。 所有其他应用程序不再能看到windir,它没有扩展到C:\ Windows。 我将完成整个安装并在没有它的情况下运行,看看TypeInvocationexception是否重新出现,或者如果不是,OS windir变量是否保持不变。

更新:

我已经确认我注意到的不良行为是由于没有指定SetEnvironment调用的目标。 使用以下我的问题似乎已经缓解:

 System.Environment.SetEnvironmentVariable("windir", System.Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User); 

当我没有设置目标时,我的Windows会话发生了非常奇怪的事情,要求我注销和登录甚至重启。 因此,除非这种更改确实是在机器或进程级别,否则不要假定使用默认值获得满意的结果