【问题标题】:VS 2012: Failed to create a 'ImageSource' from the textVS 2012:无法从文本创建“ImageSource”
【发布时间】:2012-11-15 14:14:29
【问题描述】:

我在我的资源字典中声明一个图像,然后在用户控件中显示如下:

ResourceDictionary.xaml(我在这里使用一种样式,因为我计划在用户更改他们查看的内容(即公司、员工等)时更新图像)

<ImageSource x:Key="CompanyIcon">Images/company_128.png</ImageSource>

<Style x:Key="QuickInfoIcon" TargetType="{x:Type Image}">
    <!-- Default Value -->
    <Setter Property="Source" Value="{StaticResource CompanyIcon}" />
</Style>

“Images”文件夹是“Assests”的子文件夹。 “Assests”文件夹包含我的“ResourceDictionary.xaml”文件,我知道路径是正确的,因为如果我将路径更改为“../Images/company_128.png”之类的内容,则会出现设计器错误

QuickinfoView.xaml

<UserControl x:Class="SidekickAdmin.Views.QuickInfoView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignWidth="500" Height="100"
             Background="BlanchedAlmond">

    <!-- Setup a grid to house the icon and the info -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Name="InfoIcon">
            <Image Style="{StaticResource QuickInfoIcon}" Height="50" Width="50"/>
        </Grid>
    </Grid>


</UserControl>

在 Visual Studio 2012 设计器中查看布局时,一切都显示正确,但当我运行程序时,我收到错误“发生 XamlParseException:无法从文本 'Images/employee_128.png' 创建 'ImageSource'。”在带有 ImageSource 的 ResourceDictionary 行上。

如果我将 ImageSource 更改为使用不同的图像,它会在 VS2012 设计器中按预期更新,但在尝试运行程序时会出现相同的错误。

我已在 Resource.resx 文件中将构建操作设置为“嵌入式资源”,但这并没有解决问题。

知道为什么我在尝试运行该程序时会收到 XamlParseException 吗?

作为一个附带问题,当我在我的程序中合并图像时,图像本身(文件)是否应该在 bin/debug 文件夹中的某处可见,或者此信息是否隐藏在 bin/debug 中的某个文件中?

【问题讨论】:

  • 每当我想使用资源中的图像时,我都会使用这种模式,即使资源在同一个程序集中:/VSProjectName;component/Resources/Styles。 png
  • “组件/资源”是文件路径还是VS预定义的东西?如果我的图像的路径是“/Assests/Images”,我会用什么代替你的“组件/资源”?
  • 试试这个:/VSProjectName;component/Assests/Images/company_128.png。我无法重现您的问题。
  • 在寻找其他内容时,我在解决方案资源管理器中右键单击了图像,其中一个选项是“包含在项目中”。选择此选项后,一切正常。不知道为什么这还没有包含在项目中,但希望这对其他人有帮助!我也回到了相对路径('Images/company_128.png'),它工作正常。

标签: wpf visual-studio-2012 runtime-error imagesource


【解决方案1】:

我遇到了同样的问题。

您可以尝试解决我的问题。

在解决方案资源管理器中将文件添加到您的项目中。 我建议添加文件夹太依赖于文件到项目文件的相对路径。 然后转到:

构建 -> 清洁解决方案

完成。

【讨论】:

    【解决方案2】:

    我也遇到了这个问题。这个社区或任何社区都没有提供任何建议,其中大部分是关于 PACK uri 和其他方法的逐字声明解决了这个问题。可悲的是,尽管人们在回答时表现出的所有热情,但他们中的大多数人都不知道如何解决它。

    设置:1 个解决方案,2 个项目。

    项目 1 是一个包含资源字典的类库,其中包含 BitMapSource 条目列表,这些条目指向自身下方包含的图像的本地相对路径。

    例子:

    <BitmapSource x:Key="RedoImage">Images/Redo.png</BitmapSource>
    

    项目 2 是一个 WPF 应用程序,它引用了该类库并使用 MergedDictionary 从另一个程序集中加载字典:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WPFResourceLibrary;component/Resources/images.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    在 WPF 应用程序项目的一个窗体中,我在窗体上放置了一个简单的 IMAGE 控件。在它的源属性下,我可以在 LocalResources 下从我的 images.xaml 资源字典中的 BitMapSources 列表(按键)中进行选择。进行选择,图像将出现(如预期的那样)。

    但是你瞧,在运行应用程序时,我会收到可怕的“无法从文本创建图像源...”消息。我摆弄着尝试所有的许多建议,每一个都没有成功。在运行时总是出现相同的错误,或者没有错误但没有图像。

    受够了,我制作了自己的自定义控件。我选择不覆盖图像源属性,所以我们可以在适当的地方使用它,但通过添加一个名为 ResourceName 的依赖属性来扩展它。此控件查看当前应用程序域中记录和可用的所有 BitMapSources 和 ImageSources。宾果游戏,它有效。

    代码:

    <Image x:Class="WPFResourceLibrary.Controls.ImageFromResource"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                >
    
    </Image>
    

    以及它背后的代码。

    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    
    namespace WPFResourceLibrary.Controls
    {
        /// <summary>
        /// ImageFromResource - an extension of the standard Image control that properly handles binding Resource to an Image from a Resource
        /// </summary>
        public partial class ImageFromResource : Image
        {
            public ImageFromResource()
            {
                InitializeComponent();
    
    
                DependencyPropertyDescriptor imageDescriptor = DependencyPropertyDescriptor.FromProperty(ImageFromResource.ResourceNameProperty, typeof(ImageFromResource));
                if (imageDescriptor != null)
                {
                    imageDescriptor.AddValueChanged(this, delegate { SetImage(); });
                }
            }
    
            public static DependencyProperty ResourceNameProperty = DependencyProperty.Register("ResourceName", typeof(ImageSource), typeof(ImageFromResource), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    
            [Description("Resource String of the Target Image."), Category("Appearance")]
            public ImageSource ResourceName
            {
                get { return (ImageSource)GetValue(ResourceNameProperty); }
                set
                {
                    SetValue(ResourceNameProperty, value);
                }
            }
    
            private void SetImage()
            {
                if(ResourceName != null)
                    this.Source = new BitmapImage(new Uri(ResourceName.ToString()));
            }
    
        }
    }
    

    因此,提供标准图像所期望的功能,而不会出现问题。

    注意,这是在 Visual Studio 2012 SP3 中执行的。

    保罗

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 2015-11-29
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多