图像加载多次单触屏/Xamarin工作室



这是用于单点触控(Xamarin Studio)iPad应用程序

我有一项调查,要求用户查看一张图像并对其进行评分,共有140张图像,它们是随机显示的。我将这些图像放在一个文件夹中,并将其命名为pic1.jpg、pic2.jpg、pic3.jpg等。在调查开始时,我随机生成了一个1-140的数字数组,确保不会有两次相同的数字(这已经过验证,并且正在起作用……)。然后我浏览该数组,并按随机顺序显示图像。

iOS7更新后,我遇到了一个问题,出现了错误的图像。它将在一次测试中显示多次。我已经调试过了,只是出现了错误的图像,几乎就像这个图像被另一个图像取代了。。。例如,当图像81应该出现时,图像94实际出现了。这在140张图片中发生了12次。。。

这是评级代码:

public override void ViewDidLoad ()
{
int[] Num2 = new int[141];  //array for random numbers
int ic  = 0;  //int count
rand2(ref Num2); //routine to generate random numbers...
this.btnSEG.ValueChanged += delegate(object sender, EventArgs e){ //submit the rating
ic = ic + 1; //increase the count
imgSorce.Image.Dispose(); //clear the current image
using (var pool = new NSAutoreleasePool ()) {  //put this in to prevent leaks
this.imgSorce.Image = UIImage.FromFile ("image/pic" + Num2[ic] + ".jpg");  //display the next image
};
};

我已经检查了图像文件夹中的所有图像,没有任何重复。

你知道为什么会发生这种事吗?

更新

@Krumelur要求提供生成随机编号数组的代码。。。给…

private void rand2 (ref int[] rn)
{
int i = 0;
int icount = 0;
for (i = 0; i <= 139;)
{
int n = 0;
rand3(ref n);
for(icount = 0; icount <= 139;)
{
if (n == rn[icount])
{
icount = 141;
}
icount = icount + 1;
if (icount == 140)
{
rn[i] = n;
i = i+1;
}
}
};
rn[140] = 0;
}

这是上面提到的rand3。。。

private void rand3 (ref int num)
{
Random r = new Random();
num = r.Next(1, 141);
}

就我个人而言,我认为更好的解决方案是创建数组,用序列号填充它,然后打乱数组元素。这保证了没有重复。下面是这样做的示例代码:

int[] Num2 = new int[141]; //array for random numbers
// Fill the array with numbers from 0 to 140
for (int i = 0; i < Num2.Length; i++) {
Num2[i] = i;
}
// Shuffle Array
Random rnd = new Random((int)DateTime.Now.Ticks); // seed the random number generator based on the time so you don't get the same sequence repeatedly
for (int i = Num2.Length; i > 1; i--) {
// Pick random element to swap
int j = rnd.Next(1, 141);
// Swap
int temp = Num2[j];
Num2[j] = Num2[i - 1];
Num2[i-1] = temp;
}

最新更新