从资源/ WPF访问图像的问题

时间:2011-01-23 10:03:18

标签: wpf image resources static

嗨我在访问资源中的图像时遇到问题。首先,我将一个png图像(心脏名称)添加到资源中。

在app.xaml中,hen将XAML中的Resources作为静态资源。

    <Application x:Class="Spirit.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:local="clr-namespace:Spirit.BootStraper"
                 xmlns:Converters="clr-namespace:Spirit.Converters" 
                 xmlns:Controls="clr-namespace:Spirit.Controls"
                 xmlns:props="clr-namespace:Spirit.Properties" >
        <Application.Resources>
            <ResourceDictionary>

                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary>
                        <local:MefBootStrapper x:Key="bootstrapper" />
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>

                <props:Resources x:Key="Res"/>

            </ResourceDictionary>
        </Application.Resources>
    </Application>


And use on image from resources on image source.



       <Image Name="TroubleImage"
           Style="{StaticResource InfoIcon}"
           Source="{Binding Source={StaticResource Res}, 
           Path=heart, 
           Converter={StaticResource imageToGrayConverter}}">

If I run app I get this error:

在'Spirit.Properties.Resources'类型上找不到匹配的构造函数。您可以使用Arguments或FactoryMethod指令来构造此类型。

  at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at Spirit.Views.ChatView.InitializeComponent() in c:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger_Project\Pokec_Messenger\Spirit_Caliburn_Micro_v1.0\Views\ChatView.xaml:line 1
   at Spirit.Views.ChatView..ctor() in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger_Project\Pokec_Messenger\Spirit_Caliburn_Micro_v1.0\Views\ChatView.xaml.cs:line 23

什么是坏事?

编辑:

ChatView.XAML

 <Window x:Class="Spirit.Views.ChatView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Controls="clr-namespace:Spirit.Controls" 
        xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
        Icon="/Spirit;component/Images/Logo/Icons/Ico/32.ico"
        Height="545" Width="763"
        Background="{StaticResource LightGrayBackground}">    
    <Grid   Margin="4,4,4,4">        
        <Grid Grid.Column="1" Margin="2,2,2,2">
            <Grid.RowDefinitions>
                <!--<RowDefinition Height="{Binding ElementName=InfoExpander, 
                    Path=IsExpanded, Converter={StaticResource expandedToGridLengthConverter}}" 
                               MaxHeight="220"/>-->
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="25"></RowDefinition>
                <RowDefinition Height="80"></RowDefinition>
            </Grid.RowDefinitions>

            <Image Name="TroubleImage"
                                           Style="{StaticResource InfoIcon}"
                                           Source="{Binding Source={StaticResource Res}, 
                                                            Path=heart, 
                                                            Converter={StaticResource imageToGrayConverter}
                                                  }"/>

        </Grid>
    </Grid>
</Window>

在chatview.xaml的第23行是Path =心脏图像名称是heart.png

这是我的资源屏幕,我不知道我做了什么坏事?

http://i51.tinypic.com/14wrbs1.jpg

1 个答案:

答案 0 :(得分:0)

您在App.xaml中声明<props:Resources x:Key="Res"/>,但在ChatView中发生了异常。请发布ChatView.xaml以解决您的问题。

<强>编辑:

确定。我试过了,我有点困惑。我有几次相同的异常,我可以肯定你应该在ChatView.xaml中放置<props:Resources x:Key="Res"/>。此外,我建议您将此声明放在<Window.Resources>部分的末尾。

已编辑2

天哪,我绝对是傻瓜。问题很明显!你根本不应该将Resources作为StaticResource! Properties.Resources类的实例是自动创建的!此类公开静态属性。所以你必须通过{x:Static}绑定:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    xmlns:self="clr-namespace:WpfApplication1"
    xmlns:props="clr-namespace:WpfApplication1.Properties">
<Window.Resources>
    <self:XmlConverter x:Key="Conv"/>
</Window.Resources>
<StackPanel>
    <Image Source="{Binding Source={x:Static props:Resources.dossier_ardoise_images}, Converter={StaticResource Conv}}"/>
</StackPanel>