【发布时间】:2017-06-22 09:34:22
【问题描述】:
我有一个包含72 项目的列表。该程序有点像Tinder,其中显示了图片和一些文字。
我希望这个List 是随机的,但不是第一个“卡片”和最后一个“卡片”,例如。 item no. 1 & item no. 72必须保留为第一张和最后一张卡片,其余70项将随机排序。
这是我定义列表的sn-p代码
public class MainPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
List<CardStackView.Item> items = new List<CardStackView.Item>();
public List<CardStackView.Item> ItemsList
{
get { return items; }
set { if (items == value) { return; } items = value; OnPropertyChanged(); }
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
protected virtual void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
field = value;
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public MainPageViewModel()
{
items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 1 });
items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 2 });
items.Add ........
items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 72 });
}
}
我可以在它们都在一个列表中时这样做,还是应该创建一个多维数组。其中索引 1 是 item no. 1,索引 2 是 Randomized List,索引 3 是 item no. 72。如果这是一个合适的解决方案,我将如何在我的卡片上显示它们。
我一直在研究类似Randomize a List<T> 和Best way to randomize an array with .NET 的问题,但没有成功。
【问题讨论】:
-
您正在制作列表,那么为什么不列出您想要随机播放的项目,然后根据您已经链接到的问题的答案将它们随机播放,然后当您完成时改组将您的静态 Item#1 和 Item#72 添加到适当的位置?
-
@Mashton 哦,是的,为什么我没有想到那个 facepalm,我会试试的 :)
标签: c# list xamarin random xamarin.forms