locked
Unable to programmatically load a resource at design-time RRS feed

  • Question

  • I defined a fake view model to be used by a view in a WPF application at design time (Visual Studio 2012, .NET 4.5). The view model references an image that is a part of the project content. As long as I put the image in the root project folder, it is successfully loaded at design-time using the following URI convention:

    var Image = new BitmapImage(new Uri(@"pack://application:,,,/MyAppAssemblyName;component/MyImage.png"));

    The image is marked as embedded resource (I actually tried to mark it as content and it also loaded fine).

    But I want to use it from the Sample project subfolder, so I changed the code above like this:

    var Image = new BitmapImage(new Uri(@"pack://application:,,,/MyAppAssemblyName;component/Samples/MyImage.png"));

    Then it stops working. Whatever I try (content, embedded resource etc.), nothing is loaded from the project's Samples subfolder. This is very strange since I see various examples of using subfolders, although I haven't found examples for design-time. I also tried to use pack scheme siteoforigin, but then Designer tries to load images from Visual Studio application folder.

    Any tips are appreciated.


    Vagif Abilov

    Tuesday, March 26, 2013 7:23 PM

Answers

  • Hi Vaqif Abilov,

    If you would like to embed the image into your application, try setting the Build Action to Resource.

    Not all code path are executed by desginer. I tried with the following code in VS2012 and it works fine.

    <Window x:Class="WpfApplication14.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"  xmlns:local="clr-namespace:WpfApplication14"
            >
        <Grid>
            <Image Width="300" Name="img" Source="{x:Static local:MainWindow.Image}" />
        </Grid>
    </Window>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            public static BitmapImage Image = new BitmapImage(new Uri(@"pack://application:,,,/WpfApplication14;component/subfolder/Start.jpg"));
        }

    Also, try restarting the Visual Studio to make sure the designer reloads the image.

    If the issue presists, please post a minimum sample that reproduce the issue and I can take a look at it.

    Best regards,


    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, March 28, 2013 1:26 AM
    Wednesday, March 27, 2013 2:57 AM
  • I have rerun the tests changing character casing and restarting Visual Studio and can now confirm that character casing doesn't matter. Apparently it failed earlier because VS Designer didn't always catch up with recent changes, but restart helped.

    Vagif Abilov

    • Marked as answer by Min Zhu Thursday, March 28, 2013 1:26 AM
    Wednesday, March 27, 2013 3:55 PM

All replies

  • Hi Vaqif Abilov,

    If you would like to embed the image into your application, try setting the Build Action to Resource.

    Not all code path are executed by desginer. I tried with the following code in VS2012 and it works fine.

    <Window x:Class="WpfApplication14.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"  xmlns:local="clr-namespace:WpfApplication14"
            >
        <Grid>
            <Image Width="300" Name="img" Source="{x:Static local:MainWindow.Image}" />
        </Grid>
    </Window>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            public static BitmapImage Image = new BitmapImage(new Uri(@"pack://application:,,,/WpfApplication14;component/subfolder/Start.jpg"));
        }

    Also, try restarting the Visual Studio to make sure the designer reloads the image.

    If the issue presists, please post a minimum sample that reproduce the issue and I can take a look at it.

    Best regards,


    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, March 28, 2013 1:26 AM
    Wednesday, March 27, 2013 2:57 AM
  • Hello Min, thank you for your help.

    I think I've finally got it. The problem lied in character casing. Here's my project structure:

    MyProject
        - Samples
            - MyImage.png

    The following URI works:

    pack://application:,,,/MyProject;component/samples/MyImage.png

    These don't:

    pack://application:,,,/MyProject;component/Samples/MyImage.png // DOESN'T WORK!
    pack://application:,,,/MyProject;component/samples/myimage.png // DOESN'T WORK!

    I haven't found a description for such behavior, but apparently to make Visual Studio Designer find the referenced resource the subfolder name must be converted to lowercase and the actual file name must preserve its case. Cost my quite some time to figure this out.


    Vagif Abilov

    Wednesday, March 27, 2013 7:34 AM
  • Strange. I change the forlder name to begin with uppercase and it still works.

    What's your VS version? Mine is Version 11.0.51106.01 Update 1. Maybe it's an issue fixed in Update1.


    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 27, 2013 8:00 AM
  • Mine is VS 2012 with Update 1 (can't check the exact version number right now). Did you restart the Visual Studio after changing the folder name? I've found that sometimes VS didn't catch up with changes immediately, which also caused some pain. I will do more tests later today with restarting VS after every change.

    Vagif Abilov

    Wednesday, March 27, 2013 8:11 AM
  • Yes, I restarted VS.

    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 27, 2013 8:32 AM
  • Then I will do more testing this afternoon. With full restart after each change.

    Vagif Abilov

    Wednesday, March 27, 2013 8:36 AM
  • I have rerun the tests changing character casing and restarting Visual Studio and can now confirm that character casing doesn't matter. Apparently it failed earlier because VS Designer didn't always catch up with recent changes, but restart helped.

    Vagif Abilov

    • Marked as answer by Min Zhu Thursday, March 28, 2013 1:26 AM
    Wednesday, March 27, 2013 3:55 PM
  • Thanks for the update.

    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 1:26 AM