WPF是否讨厌猫的图像?或:为什么我可以引用同一文件夹中的一个,但不能引用另一个图像?

贾尼斯F

我正在开发WPF应用程序,有时会加载图像。该图像是一个.png文件,位于我的Visual Studio项目的一个文件夹中。该文件的构建操作设置为Resource这种特殊的形象并不仅仅局限于此。在相同的文件夹中还有其他几个PNG,它们的构建动作也相同。在XAML方面,ImageSource-Element的-PropertyImageBrush绑定到Uri视图模型-Property。

现在到了奇怪的部分;这是设置Uri我的视图模型-Property的代码行

ViewModel.Image = new Uri(@"/Images/RandomIcon.png", UriKind.Relative);

但是,如果我将Uri更改为指向为测试目的而选择经过仔细搜索的cat图像,则会在调试控制台中得到一个异常堆栈,告诉我找不到该图像:

ViewModel.Image = new Uri(@"/Images/testcat.png", UriKind.Relative);

同样,图像位于同一文件夹中,其构建操作属性为Resource我能想到的唯一显着区别是其他图像是图标,比我的testcat小得多(2.5KB和140KB)。

调试控制台上显示的异常:

System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '/Images/testcat.png' (type 'Uri'); fallback value will be used, if available. BindingExpression:Path=Image; DataItem='ViewModel' (HashCode=55110660); target element is 'ImageBrush' (HashCode=51947944); target property is 'ImageSource' (type 'ImageSource') IOException:'System.IO.IOException: The Resource "images/testcat.png" cannot be found.
   at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
   at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
   at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
   at System.IO.Packaging.PackWebResponse.GetResponseStream()
   at System.IO.Packaging.PackWebResponse.get_ContentType()
   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.BitmapFrame.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy)
   at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

这激发了我的问题:WPF是否讨厌猫的图像?或:为什么我可以引用一个而不引用另一个图像?

编辑:尝试过狗图像后,我可以肯定地说WPF不会歧视猫。更严重的是,我能够在已接受的答案的帮助下解决我的问题,但我仍然不知道为什么以前不起作用。

谢里登

根据此处提供的少量信息很难确定您做错了什么。但是,这无关紧要,因为Image在WPF中显示s很简单。我的第一个技巧是根本不使用Uri该类,而仅使用string文件路径的表示来代替。

我还建议您习惯使用WPF中Pack URI来引用文件...以这个示例为例:

<Image Source="{Binding DisplayedImagePath}" />

...

public string DisplayedImagePath
{
    get { return @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"; }
}

尽管(看起来似乎已经完成了)您确实应该将图像添加到Images项目根目录中的文件夹中,并在Visual Studio的“属性”窗口中将其“构建操作”设置为“资源...然后您可以使用此格式访问它们:

public string DisplayedImagePath
{
    get { return "/AssemblyName;component/Images/ImageName.png"; }
}

在.NET 4中,上述Image.Source值将起作用。但是,Microsoft在.NET 4.5中进行了一些可怕的更改,破坏了许多不同的东西,因此在.NET 4.5中,您需要使用如下完整pack路径:

<Image Source="pack://application:,,,/AssemblyName;component/Images/ImageName.png">

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档