c# - WPF 是否讨厌猫图像?或 : Why can I reference one, 但不是来自同一文件夹的其他图像?

标签 c# wpf image

我正在开发 WPF 应用程序,有时我正在加载图像。该图像是一个 .png 文件,位于我的 Visual Studio 项目中的一个文件夹中。该文件的构建操作设置为 Resource。这个特殊的形象并不孤单;在同一个文件夹中还有其他几个 PNG,具有相同的构建操作。在 XAML 端,ImageSource - ImageBrush 元素的属性绑定(bind)到 Uri - viewmodel 的属性。

现在是奇怪的部分;这是设置我的 View 模型的 Uri-Property 的代码行:

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

但是如果我将 Uri 更改为指向 carefully googled cat image我选择用于测试目的,我在调试控制台中得到一个异常堆栈,告诉我找不到图像:

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

同样,图像位于同一文件夹中,它们的构建操作属性是 Resource。我能想到的唯一显着区别是其他图像是图标,比我的测试猫小得多(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 不会歧视猫。更严重的是,我能够在接受的答案的帮助下解决我的问题,但我仍然不知道为什么它以前不起作用。

最佳答案

根据您在此处提供的少量信息,很难准确地确定您做错了什么。但是,这应该无关紧要,因为在 WPF 中显示 Image 很简单。我的第一个建议是完全使用 Uri 类,而是简单地使用文件路径的 string 表示。

我还建议您习惯 Pack URIs in WPF用于引用文件...举个例子:

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

...

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

尽管(正如您所做的那样)您确实应该将图像添加到项目根目录中名为 Images 的文件夹中,并将它们的Build Action 设置为 < Visual Studio 中属性窗口中的 em>Resource...然后您可以使用以下格式访问它们:

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">

关于c# - WPF 是否讨厌猫图像?或 : Why can I reference one, 但不是来自同一文件夹的其他图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884623/

相关文章:

c# - 动态对象智能感知

c# - SDL Tridion 事件订阅取消订阅问题

c# - 获取 View 模型中的选定选项卡(wpf)

.net - .NET Framework 4 的向后兼容性

image - Golang 图像 ColorModel()

ios - UICollectionView 右侧高度单元格重叠

c# - 有没有办法在单个应用程序中为一个 WCF 调用指定 TLS 1.0,为另一个 WCF 或 WebClient 调用指定 TLS 1.2?

c# - 领域类应该如何设计?

c# - 如何调试命令

php - 通知: Undefined index 'upload'