HideableToolbarItem.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Threading.Tasks;
  3. using Xamarin.Forms;
  4. namespace InABox.Mobile
  5. {
  6. public class HideableToolbarItem : ToolbarItem
  7. {
  8. public HideableToolbarItem() : base()
  9. {
  10. this.InitVisibility();
  11. }
  12. private async void InitVisibility()
  13. {
  14. await Task.Delay(100);
  15. OnIsVisibleChanged(this, false, IsVisible);
  16. }
  17. //public ContentPage Parent { set; get; }
  18. public bool IsVisible
  19. {
  20. get { return (bool)GetValue(IsVisibleProperty); }
  21. set { SetValue(IsVisibleProperty, value); }
  22. }
  23. public static BindableProperty IsVisibleProperty =
  24. BindableProperty.Create<HideableToolbarItem, bool>(o => o.IsVisible, false, propertyChanged: OnIsVisibleChanged);
  25. private static void OnIsVisibleChanged(BindableObject bindable, bool oldvalue, bool newvalue)
  26. {
  27. var item = bindable as HideableToolbarItem;
  28. var parent = item.Parent as ContentPage;
  29. if (parent == null)
  30. return;
  31. var items = parent.ToolbarItems;
  32. if (newvalue && !items.Contains(item))
  33. {
  34. items.Add(item);
  35. }
  36. else if (!newvalue && items.Contains(item))
  37. {
  38. items.Remove(item);
  39. }
  40. }
  41. }
  42. }