WPF 应用程序的单元测试失败,出现 NotSupportedException “无法识别 Uri 前缀";

编程入门 行业动态 更新时间:2024-10-28 02:26:07
本文介绍了WPF 应用程序的单元测试失败,出现 NotSupportedException “无法识别 Uri 前缀";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在编写单元测试,在这个位置测试失败,并显示 NotSupportedException无法识别 URI 前缀".经过小规模研究,我注册了pack"Uri 方案,但没有帮助.

I'm currently writing unit tests and at this position the tests fails with a NotSupportedException "The URI prefix is not recognized" After small research I have registered the "pack" Uri scheme, but it dosn't helps.

return _WaitImageThumbnail ?? (_WaitImageThumbnail = new BitmapImage(new Uri("pack://application:,,,/MyAssemblyName;component/images/DefaultThumbnailLoading.png")));

堆栈跟踪:

at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase) at System.Net.WebRequest.Create(Uri requestUri) at MS.Internal.WpfWebRequestHelper.CreateRequest(Uri uri) at System.IO.Packaging.PackWebRequest.GetRequest(Boolean allowPseudoRequest) at System.IO.Packaging.PackWebRequest.GetResponse() at MS.Internal.WpfWebRequestHelper.GetResponse(WebRequest request) at MS.Internal.WpfWebRequestHelper.CreateRequestAndGetResponse(Uri uri) at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle) at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() at System.Windows.Media.Imaging.BitmapImage.EndInit() at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource, RequestCachePolicy uriCachePolicy) at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource) ...

问题:如何加载图片,为什么会出现这个异常?

Question: How can I load the image, and why this exception occurs?

推荐答案

为了加载用于单元测试的图像,您必须执行一些操作.

There are a few things you will have to do in order to load the image for your unit test.

您可以按照之前的 SO 问题注册包 URI 打包 URL 和单元测试 或初始化一个 WPF 应用程序,它将为您注册 WPF 框架组件.我通常在程序集初始化阶段执行此操作.

You can register the pack URI as per the previous SO question Pack Urls and Unit Testing or initialize a WPF Application which will register the WPF framework components for you. I typically do this during the assembly initialization stage.

[AssemblyInitialize] public static void InitializeTestAssembly(TestContext ctx) { if (Application.Current == null) new Application(); }

将图像嵌入为资源或设置为部署项

为了使用您在上面概述的包 uri,必须将图像设置为资源,以便将其烘焙到您的程序集中.如果您不用作资源,请将其更改为复制到输出目录的内容,然后配置测试环境以使用测试部署映像:

Embed the Image as a Resource or Setup as Deployment Item

In order to use the pack uri as you've outlined above, the image must be set as a Resource so that it gets baked into your assembly. If you're not using as a Resource, change it to Content that gets copied to the output directory and then configure the test environment to deploy the image with the test:

[DeploymentItem("/images/DefaultThumbnailLoading.png")] [TestMethod] public void WhenPerformingLongOperation_ShouldDisbleProgressIndicator() { // test here }

更多推荐

WPF 应用程序的单元测试失败,出现 NotSupportedException “无法识别 Uri 前缀";

本文发布于:2023-11-09 15:23:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前缀   应用程序   单元测试   无法识别   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!