N.M.V.
N.M.V.

Reputation: 45

C# WPF Path in XAML to use when loading files into resource dictionary

I have the following resource dictionary in my xaml code and I'd like to shorten the path so its not the entire directory. What is the correct way to specify this folder without having to spell out the entire file structure?

<UserControl.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="1" UriSource="C:/Users/Nick/Documents/Software/i Ching/iChing/iChing/Hexagram Images/1.jpg"/>
        <BitmapImage x:Key="2" UriSource="C:/Users/Nick/Documents/Software/i Ching/iChing/iChing/Hexagram Images/2.jpg"/>
        <BitmapImage x:Key="3" UriSource="C:/Users/Nick/Documents/Software/i Ching/iChing/iChing/Hexagram Images/3.jpg"/>
        <BitmapImage x:Key="4" UriSource="C:/Users/Nick/Documents/Software/i Ching/iChing/iChing/Hexagram Images/4.jpg"/>
    </ResourceDictionary>
</UserControl.Resources>

Upvotes: 0

Views: 903

Answers (2)

I think the proper way is to add them to your solution in separate folder. Set in properties build action Resource enter image description here

Upvotes: 0

lukbl
lukbl

Reputation: 1773

You can use BaseUri property of BitmapImage

<BitmapImage x:Key="1" BaseUri="{x:Static local:GlobalPaths.BasePath}"  UriSource="1.jpg"/>


public class GlobalPaths
{
   public static readonly Uri BasePath = new Uri(@"C:/Users/Nick/Documents/Software/i Ching/iChing/iChing/Hexagram Images/");
}

Or you could set path for each file like this:

<BitmapImage x:Key="1" UriSource="{x:Static local:GlobalPaths.File1}"/>

..Or set path relative to assembly using siteoforigin if that is an option. This is an example how it would look like if there was a file "1.jpg" in the same folder as application exe:

<BitmapImage x:Key="1"   UriSource="pack://siteoforigin:,,,/1.jpg"/>

Upvotes: 1

Related Questions