0

I'm trying to visually present a card on a WPF application. The cards are objects which hold two Enums: (1) _suit and (2) _value. I want that every time a card is drawn from the pile an Image element (called player1curCardSuit) in my XAML will change it's source to a corresponding card._value.

I'm trying to use Window.Resources for this. This is what I have in my XAML:

<Window.Resources>
    <Image x:Key="heart" Source="/images/heart.jpg" Height="50" Width="50" />
    <Image x:Key="club" Source="/images/club.jpg" Height="50" Width="50" />
    <Image x:Key="diamond" Source="/images/diamond.jpg" Height="50" Width="50" />
    <Image x:Key="spade" Source="/images/spade.jpg" Height="50" Width="50"/>
</Window.Resources>

and this is what I have in my VB code:

player1curCardSuit.Source = FindResource(player1.getCurCard.getSuit)

getSuit returns an object called _suit (an Enum member) of a the current card. When I run the program and draw a card I get an error:

'System.Windows.ResourceReferenceKeyNotFoundException' occurred in PresentationFramework.dll Additional information: 'spade' resource not found.

*'spade' is the Enum value I got from getSuit.

OzW
  • 848
  • 1
  • 11
  • 24

1 Answers1

0

First, transform your resources to BitmapImage objects instead of Image controls (because you don't use UI elements as resources in a typical WP application), and set the Width and Height properties on player1curCardSuit:

<Window.Resources>
    <BitmapImage x:Key="heart" UriSource="/images/heart.jpg" />
    <BitmapImage x:Key="club" UriSource="/images/club.jpg" />
    <BitmapImage x:Key="diamond" UriSource="/images/diamond.jpg" />
    <BitmapImage x:Key="spade" UriSource="/images/spade.jpg" />
</Window.Resources>

Then lookup your resources by key strings, not enum values:

Dim key As String = player1.getCurCard.getSuit.ToString()
player1curCardSuit.Source = FindResource(key)
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thanks very much - I did as you suggested, but there is still a problem. The pictures won't show! I put a breakpoint to watch the process, and I can see **key** getting the right string, and then **player1curCardSuit.Source** getting the right image path, but still no-show. I looked into the values of the image file that was called (for example club.JPG) and I found a few errors: **Name: DpiX Value: {"Cannot locate resource 'images/club.jpg'."}** **Name: DpiY Value: {"Cannot locate resource 'images/club.jpg'."}** Any idea what it means? – OzW Jul 20 '14 at 20:12
  • Make sure that the image files are part of your Visual Studio project, in a folder called `images`, and that their Build Action is set to `Resource`, as shown [here](http://stackoverflow.com/a/12693661/1136211). – Clemens Jul 20 '14 at 21:15
  • Thanks again. The problem was that I did not add the images to the project in the proper way (in Visual Studio: "Add existing item"), rather added them manually to the project folder. – OzW Jul 20 '14 at 21:31