简体   繁体   中英

UWP can't change Image source in C# code behind

I have been making a UWP Windows Phone app.

Here I added the functionality to change the language.

To do this, I add a ComboBox to select languages and an Image to display a flag.

When I tried to change the language on ComboBox I wanted to change the flag.

My code are below:

//index is the selected index of combobox
string strFlag = "ms-appx:///Assets/fl_english.png";
switch(index)
{
    case 0 :
        strFlag = "ms-appx:///Assets/fl_english.png";
        break;
    case 1 :
        strFlag = "ms-appx:///Assets/fl_french.png";
        break;
    case 2 :
        strFlag = "ms-appx:///Assets/fl_germany.png";
        break;
    ....
}

this.img_flag.Source = new BitmapImage(new Uri(strFlag));

But when I run the app it exited automatically.

I think the issue was occurred on this.img_flag.Source = new BitmapImage(new Uri(flag)); , because app works well without this section.

How do I fix this issue?

In your code, I don't see "flag" declared anywhere. Did you mean to use the "strFlag" variable you did declare? Eg

this.img_flag.Source = new BitmapImage(new Uri(strFlag));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM