如何解决使用字符串设置图像的值
| 我有一个字符串imgchng
和20个图像image1,image2,image3,
等。
imgchng的值始终是其中一张图像的名称。如何设置imgchng
所引用的当前图像的值?例如,用户将“ 0”的值设置为“ 4”。我该如何告诉ѭ4的出处改变? imgchng.Source = (source goes here);
不起作用,因为那会设置字符串的属性,而不是图像。我知道如何设置图像的来源,而不是如何设置字符串所指的任何图像的来源。是为了避免冗长的“ 7”语句长于1000行,例如下面的示例:
if (textBlock2.Text == \"First User Selection\")
{
if (imgchng == \"image1\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/FirstImg.png\"));
image1.Source = bmp;
}
else if (imgchng == \"image2\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/FirstImg.png\"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
else if (textBlock2.Text == \"Second User Selection\")
{
if (imgchng == \"image1\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/SecondImg.png\"));
image1.Source = bmp;
}
else if (imgchng == \"image2\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/SecondImg.png\"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
else if (textBlock2.Text == \"Third User Selection\")
{
if (imgchng == \"image1\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/ThirdImg.png\"));
image1.Source = bmp;
}
else if (imgchng == \"image2\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/ThirdImg.png\"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
else if (textBlock2.Text == \"Fourth User Selection\")
{
if (imgchng == \"image1\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/FourthImg.png\"));
image1.Source = bmp;
}
else if (imgchng == \"image2\")
{
BitmapImage bmp = new BitmapImage(new Uri(\"/Images/FourthImg.png\"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
就像@ctacke所说,基本上我想做的是给定字符串\'image1 \',我如何获取名为\'image1 \'的控件的实例?
解决方法
声明一个由20张图片组成的BitmapImage数组。将其绑定到UI。将用户选择作为整数。使用索引(显然是用户输入-1)从数组访问图像。更改该图像的源。这样可以解决您的问题吗?
,
要设置图像的来源,您必须执行以下操作。
BitmapImage bmp=new BitmapImage(new Uri(\"your image name will go here\"));
image.Source=bmp;
希望这可以帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。