Problem with access to image from resources / WPF
Hi I have problem to access to images in resources. Frist I add one png image (name of heart) to resources.
In app.xaml hen put Resources in XAML as static resource.
<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:
No matching constructor found on type 'Spirit.Properties.Resources'. You can use the Arguments or FactoryMethod directives to construct this type.
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
What is bad?
EDITED:
ChatView.XAML
<Window x:Class="Spirit.Views.ChatView"
xmlns="http://sche开发者_如何学运维mas.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>
On line 23 in chatview.xaml is Path=heart image name is heart.png
Here is screen from my resources, I don’t know what I do bad?
http://i51.tinypic.com/14wrbs1.jpg
You are declaring <props:Resources x:Key="Res"/>
in App.xaml, but exception occured in ChatView. Please, post ChatView.xaml in order to solve your problem.
EDITED:
Ok. I've tried and I am a little bit confused. I've got the same exception several times and I can assume for sure that you should place <props:Resources x:Key="Res"/>
in ChatView.xaml. Moreover I would recommend you to place this declaration at the end of <Window.Resources>
section.
EDITED 2
Oh my god, I was absolutely stupid. The problem is obvious! You should not put Resources as StaticResource at all! The instance of Properties.Resources class is created automatically! This class exposes static properties. So you have to bind via {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>
精彩评论