I switch images in the c++ code behind part of a “XAML for Windows Embedded (Compact 2013)” project (also known as "Silverlight for Windows Embedded") like described in this answer.
I use the numbers as URIs that are listed in the generated file PROJECTNAMEGenerated.rc2
. It looks like this:
IDR_PROJ_APP SLWE_XAML DISCARDABLE "XamlPack\\App.Compressed"
IDR_PROJ_DISCLAIMERPAGE SLWE_XAML DISCARDABLE "XamlPack\\DisclaimerPage.Compressed"
IDR_PROJ_EASTEREGGPAGE SLWE_XAML DISCARDABLE "XamlPack\\EasterEggPage.Compressed"
IDR_PROJ_MAINPAGE SLWE_XAML DISCARDABLE "XamlPack\\MainPage.Compressed"
IDR_PROJ_MEASUREMENTPAGE SLWE_XAML DISCARDABLE "XamlPack\\MeasurementPage.Compressed"
105 XAML_RESOURCE DISCARDABLE "..\\Xaml\\Ressources\\BatteryChargerError.png"
106 XAML_RESOURCE DISCARDABLE "..\\Xaml\\Ressources\\BatteryHigh.png"
107 XAML_RESOURCE DISCARDABLE "..\\Xaml\\Ressources\\BatteryLow.png"
108 XAML_RESOURCE DISCARDABLE "..\\Xaml\\Ressources\\disclaimer.png"
109 XAML_RESOURCE DISCARDABLE "..\\Xaml\\Ressources\\easter.jpg"
And it looks like the numbers will change if I add a page or another image.
Any idea how I get a robust URI that I can use for IXRBitmapImagePtr->SetUriSource(src);
(or IXRBitmapImage.SetUriSource(src);
)?
I also tried to add the images as resources to the application and load them like this:
BSTR src = TEXT("#IDB_BATTERY_HIGH_CHARGING");
IXRApplicationPtr app;
GetXRApplicationInstance(&app);
HINSTANCE instance = App::GetHInstance();
IWICBitmap* img = 0;
HRESULT res = app->LoadImageFromResource(instance,src, L"PNG", &img);
IXRBitmapImagePtr bitmapImage;
app->CreateObject(IID_IXRBitmapImage, &bitmapImage);
res = bitmapImage->SetSource(img);
m_pBatteryStateImage->SetSource(bitmapImage);
This sets a different image - but the wrong one (and always the same wrong one, no matter which src
I define).
I found a solution.
Add your resources again to the "standard" resource file (AppName.rc) and give them an id (you don't have to copy the file, you can use the original path).
e.g.:
IDB_ERROR PNG "..\\Xaml\\Ressources\\Error.png"
Then you can use this id:
int src = IDB_ERROR;
IXRApplicationPtr app;
GetXRApplicationInstance(&app);
HINSTANCE instance = App::GetHInstance();
IWICBitmap* img = 0;
HRESULT res = app->LoadImageFromResource(instance, MAKEINTRESOURCE(src), L"PNG", &img);
IXRBitmapImagePtr bitmapImage;
app->CreateObject(IID_IXRBitmapImage, &bitmapImage);
res = bitmapImage->SetSource(img);
m_pBatteryStateImage->SetSource(bitmapImage);
The "Silverlight for Windows Embedded Developer's Guide" says it can be done like that (but I didn't try it):
bitmapImage->SetUriSource(L"Assets/ninthAve.png");
And mybe you have to add a "#":
bitmapImage->SetUriSource(L"#Assets/ninthAve.png");