我有一個名單與其中72
項目。該程序有點像Tinder
,其中顯示了圖片和一些文本。隨機化一個列表,但範圍
我想這個List
是隨機的,但不是第一個「卡」和最後一個「卡」,例如。 item no. 1 & item no. 72
這些必須保留爲第一張和最後一張牌,其餘70個項目將按隨機順序排序。
這裏是我的代碼片段,我定義列表
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,但我沒有成功。
您正在列表爲什麼不使你想洗牌的項目清單,然後shuffle中他們基礎上,回答你已經鏈接到的問題,然後當你完成洗牌後,將靜態項目#1和項目#72添加到適當的位置? – Mashton
@Mashton哦耶爲什麼我沒有想到* facepalm *,我會試試:) – TheGejr