MobilePageStack.xaml.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. namespace InABox.Mobile
  11. {
  12. [XamlCompilation(XamlCompilationOptions.Compile)]
  13. public partial class MobilePageStack
  14. {
  15. public IList<MobilePageStackItem> Items { get; private set; }
  16. private MobilePageStackItem _current;
  17. public MobilePageStackItem SelectedItem
  18. {
  19. get => _current;
  20. set
  21. {
  22. if (_current != null)
  23. _current.DoDisappearing();
  24. _current = value;
  25. Content = _current?.Content;
  26. _current?.DoAppearing();
  27. SelectionChanged?.Invoke(this, EventArgs.Empty);
  28. }
  29. }
  30. public event EventHandler SelectionChanged;
  31. public MobilePageStack()
  32. {
  33. var items = new ObservableCollection<MobilePageStackItem>();
  34. items.CollectionChanged += ItemsChanged;
  35. Items = items;
  36. InitializeComponent();
  37. }
  38. private void ItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
  39. {
  40. Device.BeginInvokeOnMainThread(() =>
  41. {
  42. SelectedItem ??= Items.FirstOrDefault();
  43. });
  44. }
  45. }
  46. }