resource dictionary
-
vendredi 6 avril 2012 19:45
Hello,
How do i use ResourceDictionary?
I created a xaml file with resourceDictionary with ImageSource
Than in my UserControl i must to mention a key for the resourceDictionary (otherwise it doesn't compile):
<ResourceDictionary x:Key="resourceDictionary"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/AQUA.ClientServices.Presentation;component/Images/ImageResources.xaml"> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
But when i am trying to use one of the resources in the dictionary it says the resource key can't be found
Toutes les réponses
-
samedi 7 avril 2012 05:20
First do this, import your image into your project. Then on the form you want to use it. The one that has the <Image> control. Click on the Source property in the properties page over on the far right, there will be a drop down box that looks in the project for images. Allow the designer to find the image and put it into that control on that control.
Then you can click on it again, the source property, and chose the Extract value to resource which will let you pick the current control or the App.xaml
JP Cowboy Coders Unite!
-
samedi 7 avril 2012 06:56I want to define and use the resource dictionary in the scope of the user control. Also the image source is chosen according to a binding value (using style setters) . So how can i still do it?
-
samedi 7 avril 2012 09:36
Hi,
Can you just confirm that you have your images build action set to resource and not the default content.
If I've set the build action to resource I can load up images correctly (using the code you provided).
If that doesn't work then maybe you can provide some more code to give a better idea of whats going on in your app.
Thanks.
-
samedi 7 avril 2012 09:54
Yes- they are defines as resource.
But how do i specify the image source static resource to use the resource dictionary with specified key? and inside this dictionary to use specific imageSource (with key)?
-
samedi 7 avril 2012 10:44
Hi,
I've defined it like so in my main window -
<Image Source="{StaticResource imgOne}" />
and then in my images.xaml (resource dictionary) I have a bitmapimage like so -
<BitmapImage x:Key="imgOne" UriSource="Images/dog1.jpg" />
in VS I get a warning that the statciresource imgOne could not be resolved but if I run the app the image does get displayed. Thats just a VS2010 bug. Its correct xaml.
I'm not sure if that answered your question? let us know if you want to know something else.
-
samedi 7 avril 2012 11:05
Right,
But i receive an exception on the loading of the application.
my resourcedictionary :
<ResourceDictionary x:Key="resourceDictionary"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/AQUA.ClientServices.Presentation;component/Images/ImageResources.xaml"> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
and i use one of the resources like this:
<Image Source="{StaticResource MIDASRequestStuckImg}"/>
The resource itself:
<ResourceDictionary x:Class="AQUA.ClientServices.Presentation.Images.ImageResources" 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" > <ImageSource x:Key="MIDASRequestStuckImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/MIDASRequestStuck.png</ImageSource>
How do i specify the Image to use the resourceDictionary with the specified key?
-
samedi 7 avril 2012 11:13
is there a reason why you are using an x:key on the resource dictionary?
Thats probably the reason your getting the error. if you remove it the app probably compiles and displays your image.
-
samedi 7 avril 2012 11:26
No- if i remove the key . it doesn't compile. It gives this error-
Error 3 All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.
-
samedi 7 avril 2012 11:31
That error is a bit more helpful.
In your resource dictionary are there other styles etc or in youruser control?
If your styles don't have an x:key attribute then you need to specify a TargetType e.g.
if you have a stackpanel with an embedded style -
<StackPanel.Resources> <Style> <Style.Triggers> <Trigger Property="StackPanel.IsEnabled" Value="False"> <Setter Property="StackPanel.Background" Value="#FFDADECE" /> </Trigger> <Trigger Property="StackPanel.IsEnabled" Value="True"> <Setter Property="StackPanel.Background" Value="LigthBlue" /> </Trigger> </Style.Triggers> </Style>
then you need to add a target type -
<StackPanel.Resources> <Style TargetType="{x:Type StackPanel}"> <Style.Triggers> <Trigger Property="StackPanel.IsEnabled" Value="False"> <Setter Property="StackPanel.Background" Value="#FFDADECE" /> </Trigger> <Trigger Property="StackPanel.IsEnabled" Value="True"> <Setter Property="StackPanel.Background" Value="LigthBlue" /> </Trigger> </Style.Triggers> </Style>
Look through your xaml. Try commenting out sections of it and then we can at 1st see if it is the actual image resource that causing the problem (I dont think it is). and then we can work on the other styles etc later.
-
samedi 7 avril 2012 11:36
Hi . My resource dictionary is:
<ResourceDictionary x:Class="AQUA.ClientServices.Presentation.Images.ImageResources" 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" > <ImageSource x:Key="MIDASRequestStuckImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/MIDASRequestStuck.png</ImageSource> <ImageSource x:Key="MIDASRequestArrivedImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/RequestArrivedToMIDAS.png</ImageSource> <ImageSource x:Key="RequestReceivedImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/RequestReceived.png</ImageSource> <ImageSource x:Key="RequestStuckImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/RequestStuck.png</ImageSource> <ImageSource x:Key="RequestStuckImgSendingResultsImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/SendingResults.png</ImageSource> <ImageSource x:Key="SendingResultsMoreImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/SendingResultsMore.png</ImageSource> <ImageSource x:Key="FinishedImg">/AQUA.ClientServices.Presentation;component/Images/ProgressImages/Finished.png</ImageSource> </ResourceDictionary >
and the UserControl where i want to use this is
<UserControl x:Class="AQUA.ClientServices.Presentation.Documents.ProgressBar.AnalysisRunProgressUserControl" 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" xmlns:Converters="clr-namespace:AQUA.ClientServices.Client.Converters;assembly=AQUA.ClientServices.Client" mc:Ignorable="d" BorderBrush="Transparent" Background="Transparent" BorderThickness="2" d:DesignHeight="39" d:DesignWidth="58"> <UserControl.Resources> <ResourceDictionary x:Key="ResDictionary"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/AQUA.ClientServices.Presentation;component/Images/ImageResources.xaml"> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> </UserControl>
Inside this UserControl i have a style resource defines with a targetType but it is not related to the image:
<Style TargetType="{x:Type Button}" > <Setter Property="TextBlock.Foreground" Value="White"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="TextBlock.Foreground" Value="Black"/> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type Border}"> <Setter Property="BorderBrush" Value="Transparent"/> <Style.Triggers> <DataTrigger Binding="{Binding Path=Status}" Value="DONE"> <Setter Property="BorderBrush" Value="Green"/> </DataTrigger> <DataTrigger Binding="{Binding Path=Status}" Value="ERROR"> <Setter Property="BorderBrush" Value="Coral"/> </DataTrigger> </Style.Triggers> </Style>
-
samedi 7 avril 2012 11:44
do you get this error -
''Resources' property has already been set on 'Window'.' Line number 'x' and line position 'x'.
-
samedi 7 avril 2012 12:08No I don't
-
samedi 7 avril 2012 12:11
what is the error your getting?
sorry to keep asking questions but i really don't have a clue as to whats going on in your project.
It must be something simple.
-
samedi 7 avril 2012 12:16
Well i am using Enterprise Library to catch exception so eventually i get this exception:
Unable to handle exception: 'DisplayExceptionHandler'.
without inner exceptions
And only now i noticed i get the exception you mentioned earlier:
e.Exception = {"''Resources' property has already been set on 'AnalysisRunProgressUserControl'.' Line number '14' and line position '11'."}
-
samedi 7 avril 2012 12:20BTW, i can't define the resource dictionary in my App.Xaml since it is sitting in a different project . I am using PRISM so it sits in my Shell Presentation project. This project doesn't have a reference to my modules presentation project.
-
samedi 7 avril 2012 12:26
Ok i solved this.
I moved all my resources in my UserControl inside the resourceDictionary like this:
<UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/AQUA.ClientServices.Presentation;component/Images/ImageResources.xaml"> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> <Converters:ProgressPercentStringConverter x:Key="ProgressStringConvert"/> <Style TargetType="{x:Type Button}" > <Setter Property="TextBlock.Foreground" Value="White"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="TextBlock.Foreground" Value="Black"/> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type Border}"> <Setter Property="BorderBrush" Value="Transparent"/> <Style.Triggers> <DataTrigger Binding="{Binding Path=Status}" Value="DONE"> <Setter Property="BorderBrush" Value="Green"/> </DataTrigger> <DataTrigger Binding="{Binding Path=Status}" Value="ERROR"> <Setter Property="BorderBrush" Value="Coral"/> </DataTrigger> </Style.Triggers> </Style> </ResourceDictionary> </UserControl.Resources>
- Marqué comme réponse Annabella LuoModerator lundi 16 avril 2012 19:26
-
samedi 7 avril 2012 12:30
great. good stuff.
I wouldn't have thought your solution would work. I was just writing a post to ask you to move the styles into a separate resource dictionary and then reference them using the merged resource dictionary xaml. so looks like i've learn't something as well!
-
lundi 16 avril 2012 19:27Modérateur
Thank you for your sharing, michaelgr123.
And thanks for the help from Pritesh3.
Cheers!
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us

