Change Image URL's Assembly At Runtime
-
I have a WPF that's branded four ways, say App1, App2, App3, and App4. I change the name of the output assembly like this:
App1
App2
App3
App4This results in the compiled assembly being named according to the brand. I have images all over the app like this:
In some cases, in Release builds, the images fail to appear. From what I can see, in Release builds the source can't find the assembly name, so the image fails to load. I verified this by renaming all of the output assemblies in the tags above to "App1" and it works. So I've implemented code like this:
private BitmapImage _MyImage;
public BitmapImage MyImage
{
get { return _MyImage; }
set
{
if (_MyImage != value)
{
_MyImage = value;
RaisePropertyChanged("MyImage");
}
}
}and
private void SetupBranding() { var imageFolder = "";
if BRAND_APP1
imageFolder = "App1";
elif BRAND_APP2
imageFolder = "App2";
elif BRAND_APP3
imageFolder = "App3";
elif BRAND_APP4
imageFolder = "App4";
#endif// Get the assembly name string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; // Form the path to the assembly's image folder var path = $"/{assemblyName};component/Images/Brand/{imageFolder}/My\_Image.png"; MyImage = new BitmapImage(new Uri(path, UriKind.Relative)); }
This seems to work fine, but I have do do this anywhere I want to use brand-specific images. I'm wondering of there's a better way? Is there a way to handle hard coded pack URI such has:
Source="/App1;component/Images/My_Image.png"
One other concern is that, while most of this is done in the Windows and Views, there are some URI's specified in styles. Thanks
In theory, theory and practice are the same
-
I have a WPF that's branded four ways, say App1, App2, App3, and App4. I change the name of the output assembly like this:
App1
App2
App3
App4This results in the compiled assembly being named according to the brand. I have images all over the app like this:
In some cases, in Release builds, the images fail to appear. From what I can see, in Release builds the source can't find the assembly name, so the image fails to load. I verified this by renaming all of the output assemblies in the tags above to "App1" and it works. So I've implemented code like this:
private BitmapImage _MyImage;
public BitmapImage MyImage
{
get { return _MyImage; }
set
{
if (_MyImage != value)
{
_MyImage = value;
RaisePropertyChanged("MyImage");
}
}
}and
private void SetupBranding() { var imageFolder = "";
if BRAND_APP1
imageFolder = "App1";
elif BRAND_APP2
imageFolder = "App2";
elif BRAND_APP3
imageFolder = "App3";
elif BRAND_APP4
imageFolder = "App4";
#endif// Get the assembly name string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; // Form the path to the assembly's image folder var path = $"/{assemblyName};component/Images/Brand/{imageFolder}/My\_Image.png"; MyImage = new BitmapImage(new Uri(path, UriKind.Relative)); }
This seems to work fine, but I have do do this anywhere I want to use brand-specific images. I'm wondering of there's a better way? Is there a way to handle hard coded pack URI such has:
Source="/App1;component/Images/My_Image.png"
One other concern is that, while most of this is done in the Windows and Views, there are some URI's specified in styles. Thanks
In theory, theory and practice are the same
Use relative Pack URI's. [Pack URIs - WPF .NET Framework | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/pack-uris-in-wpf?view=netframeworkdesktop-4.8)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I