TodoList.xaml.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using Xamarin.Forms;
  3. namespace DevDaysTasks
  4. {
  5. public partial class TodoList : ContentPage
  6. {
  7. TodoListViewModel viewModel;
  8. public TodoList()
  9. {
  10. InitializeComponent();
  11. BindingContext = viewModel = new TodoListViewModel();
  12. }
  13. protected override void OnAppearing()
  14. {
  15. base.OnAppearing();
  16. viewModel.RefreshCommand.Execute(false);
  17. }
  18. // Event handlers
  19. public async void OnSelected(object sender, SelectedItemChangedEventArgs e)
  20. {
  21. var list = (ListView)sender;
  22. var todo = e.SelectedItem as TodoItem;
  23. if (Device.OS != TargetPlatform.iOS && todo != null)
  24. {
  25. // Not iOS - the swipe-to-delete is discoverable there
  26. if (Device.OS == TargetPlatform.Android)
  27. {
  28. await DisplayAlert(todo.Name, "Press-and-hold to complete task " + todo.Name, "Got it!");
  29. }
  30. else
  31. {
  32. // Windows, not all platforms support the Context Actions yet
  33. if (await DisplayAlert("Mark completed?", "Do you wish to complete " + todo.Name + "?", "Complete", "Cancel"))
  34. {
  35. viewModel.CompleteCommand.Execute(todo);
  36. }
  37. }
  38. }
  39. // prevents background getting highlighted
  40. list.SelectedItem = null;
  41. }
  42. // http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/#context
  43. public void OnComplete(object sender, EventArgs e)
  44. {
  45. var mi = ((MenuItem)sender);
  46. var todo = mi.CommandParameter as TodoItem;
  47. viewModel.CompleteCommand.Execute(todo);
  48. }
  49. }
  50. }