gpt4 book ai didi

c# - 洗牌列表

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:57 25 4
gpt4 key购买 nike

我的应用程序创建了一个包含 20 个按钮的列表,这些按钮位于 mainpage.xaml 上:

private List<Button> CreateList()
{
for (int i = 0; i <= 19; i++)
{
string name = string.Format("button{0}", i+1);

Button buttt = new Button();
buttt.Name = name;
buttt.Content = i + 1;
buttt.Height = 72;
buttt.HorizontalAlignment = HorizontalAlignment.Left;
buttt.VerticalAlignment = VerticalAlignment.Top;
buttt.Width = 88;
buttt.Click += new RoutedEventHandler(this.button_Click);
GameGrid.Children.Add(buttt);

myList.Insert(i, buttt);
}

现在,如果我尝试打乱这个列表,它似乎会失去与页面上实际按钮的连接。

private void Shuffle(List<Button> list)
{
//list[1].Content = "DING!";

Random rand = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rand.Next(n + 1);
Button value = list[k];
list[k] = list[n];
list[n] = value;
}
}

请注意,如果我取消注释 //list[1].Content = "DING!"; 并注释掉此方法的其余部分,按钮的内容会在屏幕上更改。所以我假设链接在随机播放期间断开了。

所以我的问题是,当我运行这段代码时,按钮会显示,但仍然按 1 到 20 的顺序排列,而不是像我预期的那样随机打乱。

感谢您的帮助!

编辑:这是包含 Chris 建议的完整代码:

private List<Button> CreateList(List<Marginz> myMargin)
{
for (int i = 0; i <= 19; i++)
{
string name = string.Format("button{0}", i+1);

Button buttt = new Button();
buttt.Name = name;
buttt.Content = i + 1;
buttt.Height = 72;
buttt.HorizontalAlignment = HorizontalAlignment.Left;
buttt.VerticalAlignment = VerticalAlignment.Top;
buttt.Width = 88;
buttt.Click += new RoutedEventHandler(this.button_Click);
Thickness myThickness = new Thickness();
myThickness.Left = myMargin[i].left;
myThickness.Top = myMargin[i].top;
myThickness.Right = myMargin[i].right;
myThickness.Bottom = myMargin[1].bottom;
buttt.Margin = myThickness;
//GameGrid.Children.Add(buttt);

myList.Insert(i, buttt);
}

return myList;
}

这里是它的名字:

private void EasyButton_Click(object sender, RoutedEventArgs e)
{
DifficultyCanvas.Visibility = System.Windows.Visibility.Collapsed;
ReadyCanvas.Visibility = System.Windows.Visibility.Visible;

//set difficulty attributes
difficulty = "Easy";

var myMarg = CreateMarginList(marg);
var buttons = CreateList(myMarg);
Shuffle(buttons);

foreach (var button in buttons)
{
GameGrid.Children.Add(button);
}
}

编辑更多解释:关于边距。我创建了一个名为 Marginz 的类:

public class Marginz
{
public Marginz()
{
//Constructor
}
public int left { get; set; }
public int top { get; set; }
public int right { get; set; }
public int bottom { get; set; }
}

“marg”是这种类型的列表:

List<Marginz> marg = new List<Marginz>(20);

然后 CreateMarginList() 这样做:

public List<Marginz> CreateMarginList(List<Marginz> myMarg)
{
Marginz one = new Marginz();
one.left = 28;
one.top = 186;
one.right = 0;
one.bottom = 0;
myMarg.Insert(0, one);

Marginz two = new Marginz();
two.left = 133;
two.top = 186;
two.right = 0;
two.bottom = 0;
myMarg.Insert(1, two);

等一直到二十。然后 返回 myMarg;所以每个 Button 都有一个独特的边距,将它放在 Grid 中。

This is what I have right now:

最佳答案

打乱 myList 集合不会改变它们在页面上的显示顺序。这取决于您在 CreateList 方法中将它们添加到 GameGrid 的顺序。您可以做的是全部创建它们,打乱列表,然后将它们添加到 Children 列表中。

因此删除 CreateList 中的 GameGrid.Children.Add 调用(注意,我稍微调整了那里的代码,我假设您没有发布完整代码)

private List<Button> CreateList()
{
var myList = new List<Button>();

for (int i = 0; i <= 19; i++)
{
string name = string.Format("button{0}", i+1);

Button buttt = new Button();
buttt.Name = name;
buttt.Content = i + 1;
buttt.Height = 72;
buttt.HorizontalAlignment = HorizontalAlignment.Left;
buttt.VerticalAlignment = VerticalAlignment.Top;
buttt.Width = 88;
buttt.Click += new RoutedEventHandler(this.button_Click);

myList.Add(buttt);
}

return myList;
}

执行随机播放,然后添加它们:

var buttons = CreateList();
Shuffle(buttons);

foreach(var button in buttons)
{
GameGrid.Children.Add(button);
}

编辑:从您发布的完整代码来看,问题是因为所有按钮都在 Grid 控件中,它们的位置取决于它们在 它们的 Margin(控制它们在该单元格中的位置)。如果您没有明确定义它们的行/列,那么它们将被假定在第一行/列中。在这种情况下,它们的边距(未被打乱)决定了它们的位置。

我认为在这种情况下,要么用单元格构建网格,要么可能最简单:在创建列表之前简单地打乱 myMargin 列表!按钮将按顺序添加,但它们的位置是随机的。

var myMargin = CreateMargins(); //wherever that's done
Shuffle(myMargin); //you'll have to change the signature to work against List<Thickness> instead
var buttons = CreateList(myMargin); //add back the GameGrid.Children.Add call
//notice, no longer a call to shuffle the buttons

可能不是最佳解决方案,但我认为这会给您带来与您想要的相同的效果。

关于c# - 洗牌列表 <Button> 失去与 mainpage.xaml 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14307765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com