我有一个问题是在Windows手机中随机显示几个网格。 我创建了自己的代码,它工作正常,但并不简单。也许有一种更简单的方法来在Windows手机中显示随机网格。
这就是我的应用所需:
我想点击一个按钮来显示随机网格。当我再次单击时,它应显示另一个网格并隐藏前一个网格。它现在应该显示任意网格两次。
这是我自己的代码:
List<int> number = new List<int> { 1,2,3,4,5 }; //create list
private void Button_Click_2(object sender, RoutedEventArgs e)
{
int numberrandom;
Random bsd = new Random();
if (number.Count > 0) // get random number from (numberlist) without repetition
{
int fIndex = bsd.Next(0, number.Count);
numberrandom = number[fIndex];
txtbox1.Text = numberrandom.ToString(); // show random number at txtbox
number.RemoveAt(fIndex);
}
else
{
messagebox.show("no more grid show");
}
int a = Convert.ToInt32(txtbox1.Text); // convert number txtbox from string to int
int val = a;
switch (val)
{
case 1: //show grid 1
grid1.Visibility = Visibility.Visible;
grid2.Visibility = Visibility.Collapsed;
grid3.Visibility = Visibility.Collapsed;
grid4.Visibility = Visibility.Collapsed;
grid5.Visibility = Visibility.Collapsed;
break;
case 2: //show grid 2
grid1.Visibility = Visibility.Collapsed;
grid2.Visibility = Visibility.Visible;
grid3.Visibility = Visibility.Collapsed;
grid4.Visibility = Visibility.Collapsed;
grid5.Visibility = Visibility.Collapsed;
break;
case 3: //show grid 3
grid1.Visibility = Visibility.Collapsed;
grid2.Visibility = Visibility.Collapsed;
grid3.Visibility = Visibility.Visible;
grid4.Visibility = Visibility.Collapsed;
grid5.Visibility = Visibility.Collapsed;
break;
case 4: //show grid 4
grid1.Visibility = Visibility.Collapsed;
grid2.Visibility = Visibility.Collapsed;
grid3.Visibility = Visibility.Collapsed;
grid4.Visibility = Visibility.Visible;
grid5.Visibility = Visibility.Collapsed;
break;
case 5: //show grid 5
grid1.Visibility = Visibility.Collapsed;
grid2.Visibility = Visibility.Collapsed;
grid3.Visibility = Visibility.Collapsed;
grid4.Visibility = Visibility.Collapsed;
grid5.Visibility = Visibility.Visible;
break;
}
答案 0 :(得分:1)
我认为您要做的只是在开始之前对整数列表进行随机排序。
List<int> lNumbers = New List<int> {1,2,3,4,5};
var rnd = New Random();
lNumbers = lNumbers.OrderBy(ob => rnd.Next()).ToList();
然后只保留一个索引计数器变量,并在每次单击该按钮时显示列表中的下一个。
虽然我对Winodws Phone不太熟悉,但我认为你可以使用FindControl来获得正确的gridview控件来隐藏,而右键则可以取消隐藏。
希望这有帮助。
:)大卫
PS - 我对Yate先生的回答唯一不喜欢的是:foreach(var grid in grids) {
grid.Visibility = Visibility.Collapsed;
}
grids[gridIndex].Visibility = Visibility.Visible;
您无需将所有网格可见性设置为Collpased。只有最后一个:
if (gridIndex > 0) {
grids[gridIndex - 1].Visibility = Visibility.Collapsed;
}
grids[gridIndex].Visibility = Visibility.Visible;
答案 1 :(得分:1)
您的代码确实可以简化。例如,您可以将所有网格放在一个列表中并在其上循环以设置可见性:
List<int> number = new List<int> { 0, 1, 2, 3, 4 }; //create list
private void Button_Click_2(object sender, RoutedEventArgs e)
{
int numberrandom;
Random bsd = new Random();
if (number.Count > 0) // get random number from (numberlist) without repetition
{
int fIndex = bsd.Next(0, number.Count);
numberrandom = number[fIndex];
txtbox1.Text = (numberrandom + 1).ToString(); // show random number at txtbox
number.RemoveAt(fIndex);
}
else
{
MessageBox.Show("no more grid show");
}
var grids = new List<Grid> { grid1, grid2, grid3, grid4, grid5 };
for (int i = 0; i < grids.Count; i++)
{
grids[i].Visibility = i == numberrandom ? Visibility.Visible : Visibility.Collapsed;
}
}
答案 2 :(得分:1)
您可以进行一些改进:
List<Grid> grids = new List<Grid> { grid1, grid2, grid3, gridN ... };
int gridIndex = 0;
void Load() {
grids.Shuffle(); // use the extension method linked below
}
void Button_Click_2(object sender, RoutedEventArgs e) {
if (gridIndex >= grids.Count)
return;
if (gridIndex > 0) {
grids[gridIndex - 1].Visibility = Visibility.Collapsed;
}
grids[gridIndex].Visibility = Visibility.Visible;
gridIndex++;
}
您需要显示原始网格号吗?如果没有,上面的代码要简单得多。这是Shuffle()扩展方法,我是从this answer得到的:
public static void Shuffle<T>(this IList<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}