Skip to content

Instantly share code, notes, and snippets.

@ytabuchi
Last active September 5, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytabuchi/6e6a7326247f631d98900e3d26f2b58a to your computer and use it in GitHub Desktop.
Save ytabuchi/6e6a7326247f631d98900e3d26f2b58a to your computer and use it in GitHub Desktop.
Xamarin.Forms で Mvvm で ListView を作ってみた1
public class Ramen : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// シングルトンのインスタンス
/// </summary>
public static Ramen Instance { get; } = new Ramen();
/// <summary>
/// コンストラクターは隠ぺい
/// </summary>
private Ramen() { }
#region プロパティ
// ランダム選択用の配列
private string[] _ramens = { "ramen1.png", "ramen2.png", "ramen3.png", "ramen4.png", "ramen5.png", "ramen6.png", "ramen7.png", "ramen8.png", "ramen9.png" };
public ObservableCollection<RamenItem> Items { get; } = new ObservableCollection<RamenItem>();
#endregion
#region メソッド
public void Initialize()
{
this.Items.Clear();
this.Items.Insert(0, new RamenItem("Item_1", "Description_1", "ramen4.png"));
}
public void AddItem()
{
var rdm = new Random();
this.Items.Insert(0, new RamenItem(
"Item_" + rdm.Next(),
"Description_" + rdm.Next(),
_ramens[rdm.Next(0, 8)]
));
// PropertyChangedイベントを発火
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Items)));
}
public void DeleteItem()
{
if (this.Items.Count > 0)
{
this.Items.Remove(this.Items[this.Items.Count - 1]);
// PropertyChangedイベントを発火
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Items)));
}
}
#endregion
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment