我几乎写了一段代码,根据随机的int值选择要在imagebox中加载哪些图像。 我将所有图像存储在名为" projectImages"在我的解决方案资源管理器 我得到的错误是"未处理的类型' System.UriFormatException'发生在System.dll" "附加信息:无效的URI"。这可能意味着我没有正确连接链接。
提前致谢。
{
int[] ImageValues = new int[15];
ImageValues[0] = 1;
ImageValues[1] = 2;
ImageValues[2] = 3;
ImageValues[3] = 4;
ImageValues[4] = 5;
ImageValues[5] = 6;
ImageValues[6] = 7;
ImageValues[7] = 8;
ImageValues[8] = 9;
ImageValues[9] = 10;
ImageValues[10] = 11;
ImageValues[11] = 12;
ImageValues[12] = 13;
ImageValues[13] = 14;
ImageValues[14] = 15;
Random randomize = new Random();
int initialValue = randomize.Next(1, 16);
int finalValue = ImageValues[initialValue];
if(finalValue == 1)
{
leftImagebox.Source = new BitmapImage(new Uri(@"projectImages\triangle.png"));
midImagebox.Source = new BitmapImage(new Uri(@"projectImages\circle.png"));
rightImagebox.Source = new BitmapImage(new Uri(@"projectImages\square.png"));
question.Content = "Kan jij de driehoek aanwijzen?";
}
else if(finalValue == 2)
{
leftImagebox.Source = new BitmapImage(new Uri(@"projectImages\middelstestaaf.png"));
midImagebox.Source = new BitmapImage(new Uri(@"projectImages\kortestaaf.png"));
rightImagebox.Source = new BitmapImage(new Uri(@"projectImages\langestaaf.png"));
question.Content = "Kan jij de langste staaf aanwijzen?";
}
else if (finalValue == 3)
{
leftImagebox.Source = new BitmapImage(new Uri(@"projectImages\triangle.png"));
midImagebox.Source = new BitmapImage(new Uri(@"projectImages\circle.png"));
rightImagebox.Source = new BitmapImage(new Uri(@"projectImages\square.png");
question.Content = "Kan jij de cirkel aanwijzen?";
}
答案 0 :(得分:0)
您是否确定已经在项目中引用了图像资源且路径正确,如果是,请尝试
new Uri(@"YourPath", UriKind.RelativeOrAbsolute)
。
如果您使用WPF或Silverlight,我强烈建议您在项目中使用Pack Uri,here是关于pack uri的官方文档。
然后Uri应该是这样的:
new Uri(@"ms-appx:///,,,:YourPath/projectImages/triangle.png")