I have this very annoying bug that i encounter in every project:
I have a class (same namespace) where i store references to Images like this:
public class Res
{
public static BitmapImage A = new BitmapImage(new Uri(@"images\A.png", UriKind.Relative));
}
When i try to change the source of an element towards one of these static references via:
xamldefinedimage.Source = Res.A;
it does absolutely nothing. (I enclose it of course with the corresponding Dispatcher.)
Now when i assign the image source like this:
xamldefinedimage.Source = new BitmapImage(new Uri(@"images\A.png", UriKind.Relative));
it works just fine. I checked the images Properties (Ressource, Copy on Build -> look ok).
This problem drives me especially nuts because i have older projects where the reference setting works just fine. I dont want to use a workaround but a special class with static references to keep the code minimal. I really want to get to the bottom of this and understand what i am missing here once and for all. This is basically a blank project, not much has been done yet. What is the issue here, what are potential reasons the first approach is not working?
Your question is about static class but you did not defined Res class as static.
your code should be like this :
public static class Res
{
public static BitmapImage A = new BitmapImage(new Uri(@"images\A.png", UriKind.Relative));
}
Also if your images are in the App Resources then your class should be like this :
public static class Res
{
public static BitmapImage A = new BitmapImage(new Uri(@"pack://application:,,,/Images/A.png"));
}