UWP无法在C#代码后面更改图像源

时间:2018-12-28 01:52:39

标签: c# xaml uwp

我一直在制作UWP Windows Phone应用。

我在这里添加了更改语言的功能。

为此,我添加了ComboBox以选择语言,并添加了Image以显示标志。

当我尝试更改ComboBox上的语言时,我想更改标志。

我的代码如下:

//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));

但是当我运行该应用程序时,它会自动退出。

我认为该问题发生在this.img_flag.Source = new BitmapImage(new Uri(flag));上,因为没有此部分,应用运行良好。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

在您的代码中,我看不到任何地方声明的“标志”。您是要使用声明的“ strFlag”变量吗?例如

this.img_flag.Source = new BitmapImage(new Uri(strFlag));
相关问题