How do I switch an image in “XAML for Windows Embedded (Compact 2013)”

旧城冷巷雨未停 提交于 2019-12-11 17:49:22

问题


I have a project for Windows CE that uses XAML for Windows Embedded (Compact 2013) (also known as "Silverlight for Windows Embedded") for the GUI.

I defined an image in xaml and now I want to switch this image in the c++ code behind part.

How do I do this?


回答1:


I found this solution:

m_pBatteryStateImageis the image, defined in Xaml.

The URIs for the images can be found in the auto generated file PROJECTNAMEGenerated.rc2

void MainPage::SetBatteryState(BatteryStateFlags batteryState)
{

    BSTR src = GetImageSourceUri(batteryState);
    SetImage(src);
}

void MainPage::SetImage(BSTR src)
{
    IXRApplication* application;
    App::GetApplication(&application);
    //Check which uri is currently used:
    BSTR originalSrc;
    IXRImageSource* iSource;
    m_pBatteryStateImage->GetSource(&iSource);
    IXRBitmapImagePtr bmpSrc = (IXRBitmapImagePtr)iSource;
    bmpSrc->GetUriSource(&originalSrc);
    //Set new image if source uri is different
    if (wcscmp(originalSrc,src)!=0) 
    {
        IXRBitmapImagePtr bitmapImage;
        application->CreateObject(IID_IXRBitmapImage, &bitmapImage);
        bitmapImage->SetUriSource(src);
        m_pBatteryStateImage->SetSource(bitmapImage);
    }
}

BSTR MainPage::GetImageSourceUri(BatteryStateFlags batteryState)
{
    BSTR src; 
    //see PROJECTNAMEGenerated.rc2 - the numbers will change if images are added (they are alphabetically sorted).
    //TODO make it robust against changes
    if(batteryState & BatteryChargerError)
        src = TEXT("#105"); 
    else if(batteryState &  BatteryHigh)
        src = TEXT("#106");
    else if(batteryState & BatteryLow)
        src = TEXT("#109");
    else
        //Show error if nothing else matches (Should not happen)
        src = TEXT("#105");
    return src;
}


来源:https://stackoverflow.com/questions/45876545/how-do-i-switch-an-image-in-xaml-for-windows-embedded-compact-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!