WPF - 在 Window 类中对方法进行单元测试时出现 XamlParseException

发布于 2024-09-25 07:01:20 字数 650 浏览 0 评论 0原文

我正在尝试使用 MSTest 为 WPF 应用程序中的 MainWindow 类中的方法编写单元测试。但是,我得到以下异常:

System.Windows.Markup.XamlParseException:“在‘System.Windows.StaticResourceExtension’上提供值引发了异常。”行号“127”和行位置“32”。 ---> System.Exception:找不到名为“verticalLineStyle”的资源。资源名称区分大小写。

verticalLineStyle 可在 App.xaml 中包含的 ResourceDictionary 中找到。我不需要这种风格来测试我正在尝试测试的方法,但我无法克服这一点。我只是在单元测试中创建一个新的 MainWindow 实例,并尝试调用它的实例方法。当我尝试创建 MainWindow 实例时发生异常。 MainWindow 定义为公共分部类 MainWindow : Window

我该如何摆脱这个错误?在 WPF 应用程序中,Windows 中是否有一些首选的单元测试方法?

I'm trying to use MSTest to write unit tests for methods in my MainWindow class in my WPF application. However, I get the following exception:

System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '127' and line position '32'. ---> System.Exception: Cannot find resource named 'verticalLineStyle'. Resource names are case sensitive.

verticalLineStyle is found in a ResourceDictionary that is included in App.xaml. I have no need of this style to test the method I'm trying to test, but I can't get past this. I'm just creating a new instance of MainWindow in my unit test and trying to call an instance method on that. The exception occurs when I try to create the instance of MainWindow. MainWindow is defined as public partial class MainWindow : Window.

How do I get rid of this error? Is there some preferred way of unit testing methods in windows in WPF applications?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2024-10-02 07:01:20
Application.LoadComponent(
    new Uri("/MyAppAssemblyName;component/App.xaml", UriKind.Relative));

这对我有帮助。

Application.LoadComponent(
    new Uri("/MyAppAssemblyName;component/App.xaml", UriKind.Relative));

This helped me.

睡美人的小仙女 2024-10-02 07:01:20

问题是测试无法访问 App.xaml,因为应用程序未启动。您可能想查看 TestApi 以了解与测试 WPF 视觉效果相关的任何内容(链接

希望这会有所帮助!

The problem is that the test does not have access to App.xaml as the application is not started. You might want to look into TestApi for anything related to testing WPF visuals (link)

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文