WPF-XamlParseException当Window类中的单元测试方法时 [英] WPF - XamlParseException when unit testing method in Window class

查看:191
本文介绍了WPF-XamlParseException当Window类中的单元测试方法时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:'在'System.Windows.StaticResourceExtension'上提供值引发了异常.行号"127"和行位置"32". ---> System.Exception:找不到名为"verticalLineStyle"的资源.资源名称区分大小写.

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.

App.xaml中包含的ResourceDictionary中找到

verticalLineStyle.我不需要这种样式来测试我要测试的方法,但是我无法超越.我只是在单元测试中创建MainWindow的新实例,然后尝试在该实例上调用实例方法.当我尝试创建MainWindow的实例时发生异常. MainWindow被定义为public partial class MainWindow : Window.

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.

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

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

推荐答案

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

这对我有帮助.

这篇关于WPF-XamlParseException当Window类中的单元测试方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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