WPF 应用程序的单元测试失败,出现 NotSupportedException “无法识别 Uri 前缀"; [英] Unit Test of WPF Application fails with NotSupportedException "The Uri prefix is not recognized"

查看:270
本文介绍了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 前缀";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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