c# Uri 加载非确定性?
我正在尝试从文件系统上保存的文件加载一些位图图像。我有一个键和相对文件路径的字典。不幸的是,Uri 构造函数加载图像的方式似乎是不确定的。
这是我的代码:
foreach(KeyValuePair<string, string> imageLocation in _imageLocations)
{
try
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@imageLocation.Value, UriKind.Relative);
img.EndInit();
_images.Add(imageLocation.Key, img);
}
catch (Exception ex)
{
logger.Error("Error attempting to load image", ex);
}
}
不幸的是,有时 Uris 会作为相对文件 Uris 加载,有时它们会作为相对 Pack Uris 加载。似乎没有任何押韵或理由来说明哪个将以哪种方式加载。有时我会以一种方式加载所有 Uris,或者只是加载几个,或者大多数,并且每次运行代码时它都会发生变化。
有什么想法吗?
I am trying to load some BitmapImages from files held on the file system. I have a dictionary of keys and relative filepaths. Unfortunately the Uri constructor seems non deterministic in the way that it will load the images.
Here is my code:
foreach(KeyValuePair<string, string> imageLocation in _imageLocations)
{
try
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@imageLocation.Value, UriKind.Relative);
img.EndInit();
_images.Add(imageLocation.Key, img);
}
catch (Exception ex)
{
logger.Error("Error attempting to load image", ex);
}
}
Unfortunately sometimes the Uris get loaded as relative file Uris and sometimes they get loaded as relative Pack Uris. There doesn't seem to be any rhyme or reason as to which will get loaded which way. Sometimes I get all the Uris loading one way, or just a couple, or most of them, and it will change each time I run the code.
Any ideas what is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,有点... MSDN 对 UriKind 有这样的说法:
绝对 URI 的特点是对资源的完整引用(例如:http://www.contoso.com/index.html),而相对 Uri 取决于先前定义的基本 URI(例如:/index.html)
如果您跳入反射器并环顾四周,您会发现代码需要采用很多路径来解析相对 URI 应该是什么。无论如何,这并不是说它是不确定的,而是它只是许多开发人员感到沮丧的一个主要根源。您可以做的一件事是使用“BaseUriHelper”类来了解您的 uri 的解析方式。
另一方面,如果您知道您的资源存储在哪里(并且您应该知道),我建议您不用再烦恼,并使用绝对 URI 来解析您的资源。每次都有效,而且幕后不会有任何愚蠢的代码在您最意想不到的时候给您带来麻烦。
Well, sort of... MSDN has this to say about the UriKind:
Absolute URIs are characterized by a complete reference to the resource (example: http://www.contoso.com/index.html), while a relative Uri depends on a previously defined base URI (example: /index.html)
If you jump into reflector and look around you can see that there are lots of paths for the code to take to resolve what the relative URI should be. Anyhow, it isn't that its non-deterministic, it's more that it is just a major source of frustration for many developers. One thing that you can do is emply the 'BaseUriHelper' class to get to the bottom of how your uris are being resolved.
On the other hand, if you know where your resources are being stored (and you should) I would suggest that you just spare yourself the headache and use an absolute URI to resolve your resources. Works every time, and no goofy code behind the scenes to trip you up when you least expect it.
最后,我通过获取应用程序的基本目录并附加相对路径并使用绝对 URI 而不是相对 URI 解决了这个问题。
In the end I solved the problem by getting the base Directory of my app and appending the relative path to that and using an absolute URI rather than a relative one.