3

I have found some code online that I can use on my own project. It's fairly simple, but when I run the program, I get an error message that it can not find the resource.

I have tried a lot and simply can not see the error - I inserted pictures by saying: add -> new folder (right click) -> added all the images:

 int[] yourHandArray = yourHand.ToInt();
        int[] dealerHandArray = dealerHand.ToInt();

        for (int i = 0; i < yourHandArray.Length; i++)
        {
            switch (i)
            {
                case 0: Card1.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_cards/"+ (yourHandArray[i] + 1) + ".png")); break;
                case 1: Card2.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_cards/" + (yourHandArray[i] + 1) + ".png")); break;
                case 2: Card3.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_Cards/" + (yourHandArray[i] + 1) + ".png")); break;
                case 3: Card4.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_cards/" + (yourHandArray[i] + 1) + ".png")); break;
                case 4: Card5.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_cards/" + (yourHandArray[i] + 1) + ".png")); break;
                case 5: Card6.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_cards/" + (yourHandArray[i] + 1) + ".png")); break;
                case 6: Card7.Source = new BitmapImage(new Uri("pack://application:,,,/View/img_cards/" + (yourHandArray[i] + 1) + ".png")); break;
            }
        }

This is the folder I refer to

Kevin
  • 151
  • 6
  • Is the build action of the new files correctly set to "Resource"? – Klaus Gütter Nov 10 '18 at 10:18
  • 1
    Your code is far too complicated. Better put the Images elements in another array and write `Card[i].Source = new BitmapImage(...)` without the switch block. Besides that, make sure that the Build Action of the PNG files is set to Resource. – Clemens Nov 10 '18 at 10:19
  • Yes the build action of the new files er set to "Resources" but still cant be found. – Kevin Nov 10 '18 at 10:53
  • @Clemens That means I also have to change the XAML window and I can not see how to do that. – Kevin Nov 10 '18 at 10:59
  • Why exactly change the XAML? You can easily add Card1 to Card7 to an array. However, much better would be an ItemsControl with the Image element in its ItemTemplate. – Clemens Nov 10 '18 at 11:01
  • Also check if the image files are located in the correct project folders. With `pack://application:,,,/View/img_cards/...` they should be in a folder `img_cards` which itself is in a folder `View` in the root of the same project where you use them. – Clemens Nov 10 '18 at 11:04
  • They are inserted as you describe, but still can not be found. Have I perhaps overlooked something else? @Clemens – Kevin Nov 10 '18 at 11:11
  • @Clemens I have inserted a picture of my project --> Maybe you can see an error in it. – Kevin Nov 10 '18 at 11:18
  • And this in the project of the application? Or is it in a class library? – Klaus Gütter Nov 10 '18 at 11:21
  • @KlausGütter It is in a class library but the project have a reference to it and can use all the librarys cs. files and xamls. – Kevin Nov 10 '18 at 11:26
  • You could try adding the assembly name of the class library to the URI: pack://application:,,,/MyAssembly;component/View/img_cards/... – Klaus Gütter Nov 10 '18 at 11:44
  • Just to be sure... Should I replace 'MyAssembly' with the library name? And what about 'component' should it be changed to something specific? @KlausGütter – Kevin Nov 10 '18 at 12:09
  • Yes, replace MyAssembly with the assembly name of the class library, keep the "component" as it is. See https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf – Klaus Gütter Nov 10 '18 at 12:14
  • Thank you very much @KlausGütter!! It works :-) – Kevin Nov 10 '18 at 12:16

0 Answers0