WPF 窗口在启动时崩溃,或者它启动但挂起并且不呈现内容 [英] WPF window crashes on startup, or it starts but hangs and does not render contents

查看:29
本文介绍了WPF 窗口在启动时崩溃,或者它启动但挂起并且不呈现内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 应用程序在某些客户端机器上崩溃但在其他地方工作 - 每个开发人员的噩梦.

I've had a couple nasty problems with WPF applications crashing on certain client machines but working everywhere else - every developer's nightmare.

我在这里发布问题/解决方案是为了帮助其他开发人员保持理智.这些都不容易解决.

I'm posting the problems / solution here to help preserve the sanity of fellow developers. These were not easy to troubleshoot.

这两个问题都是经典的它适用于我的机器,但不适用于他们的"问题.

Both of these issues are classic "it works on my machine but not theirs" issues.

  1. WPF 应用程序在启动时崩溃.没有错误,不知道为什么.
  2. WPF 应用程序启动,窗口边框显示,但内容不绘制.它只是挂起(报告为透明窗口")点击关闭会触发崩溃报告(机器是 Windows XP).

推荐答案

1) WPF 应用程序在启动时崩溃.没有错误,不知道为什么.

首先真正找到错误的关键故障排除步骤是向 app.xaml 添加额外的错误处理.

The key troubleshooting step to actually find the error in the first place is to add additional error handling to app.xaml.

在您的 App.xaml 标头中,添加:

In your App.xaml header, add:

<Application DispatcherUnhandledException="App_DispatcherUnhandledException" />

并在您的 App.xaml.cs 中添加如下内容:

and in your App.xaml.cs, add something like:

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
    {
        log.Fatal("An unexpected application exception occurred", args.Exception);

        MessageBox.Show("An unexpected exception has occurred. Shutting down the application. Please check the log file for more details.");

        // Prevent default unhandled exception processing
        args.Handled = true;

        Environment.Exit(0);
    }

添加这个额外的调试后,我发现了错误:

After adding this additional debugging, I caught the error:

System.TypeInitializationException:System.Windows.Media.FontFamily"的类型初始值设定项引发异常.---> System.ArgumentException: 路径中有非法字符.

进一步的研究、谷歌搜索和咖啡,导致客户端机器在注册表中的字体条目中有一个尾随◻"字符的解决方案

Further research, googling, and coffee, led to the solution that the client machine had a trailing '◻' character in a font entry in the registry under

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

删除这个流氓角色解决了问题.

Removing this rogue character solved the problem.

请参阅挽救职业生涯的博文 如何使每个 WPF 应用程序崩溃".

See the career-saving blog post "How to Crash every WPF application".

2) WPF 应用程序启动,窗口边框显示,但内容不绘制.它只是挂起(报告为透明窗口")

这是另一个与字体相关的问题.我最初将其归结为 Window.xaml 中的 fontSize="16" 不能在客户端站点的一台(并且只有一台)机器上工作,就好像 WPF 拒绝以 16 号 (?) 呈现其字体一样.我删除了指定字体大小的代码并且它起作用了……但事实证明,它仅适用于我的登录配置文件.对于同事的登录配置文件在同一台机器上,它继续失败.我实际上可以以我自己的身份登录,成功运行,退出,他以他自己的身份登录,运行它,但它会因透明悬挂窗口"而失败.相同的机器,相同的权限.

This was another font-related issue. I initially boiled this down to a fontSize="16" in Window.xaml not working on one (and only one) machine at a client site, as if WPF refused to render their fonts at size 16 (?). I removed the code specifying the font size and it worked ... but as it turns out, it only worked for my login profile. It continued to fail for a colleague's login profile on the same machine machine. I could literally login as myself, run it successfully, log out, he'd log in as himself, run it, and it'd fail with the "transparent hanging window". Same machine, same permissions.

作为最后的手段,我以他的身份登录,并注意到他有一个带有放大字体的自定义主题.我将 Windows 主题改回经典主题……这解决了问题(?!?).这个问题似乎也与字体有关,但我没有确定绝对的根本原因.将主题背景改回标准主题是一种临时解决方法.

As a last resort, I logged in as him and noticed he had a custom theme set with enlarged font. I changed the windows theme back to the classic theme ... and this fixed the issue (?!?). This issue appears to also be font related, but I don't have an absolute root cause identified. Changing the theme back to standard theme is a temporary workaround.

这篇关于WPF 窗口在启动时崩溃,或者它启动但挂起并且不呈现内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆