void OnActionItemClickedImpl(IMenuItem item) { int index = item.ItemId; IMenuItemController action = ActionModeContext.ContextActions[index]; action.Activate(); }
public ToolbarButton(Context context, ToolbarItem item) : base(context) { if (item == null) { throw new ArgumentNullException("item", "you should specify a ToolbarItem"); } Item = item; Enabled = Controller.IsEnabled; Text = Item.Text; SetBackgroundColor(new Color(0, 0, 0, 0).ToAndroid()); Click += (sender, e) => Controller.Activate(); Item.PropertyChanged += HandlePropertyChanged; }
Native.Button GetToolbarButton(ToolbarButtonPosition position) { ToolbarItem item = _toolbarTracker.ToolbarItems.Where( i => (position == ToolbarButtonPosition.Right && i.Order <= ToolbarItemOrder.Primary) || (position == ToolbarButtonPosition.Left && i.Order == ToolbarItemOrder.Secondary)) .OrderBy(i => i.Priority).FirstOrDefault(); if (item == default(ToolbarItem)) { return(null); } Native.Button button = new Native.Button(Forms.NativeParent); button.Clicked += (s, e) => { IMenuItemController control = item; control.Activate(); }; button.Text = item.Text; button.BackgroundColor = Color.Transparent.ToNative(); if (string.IsNullOrEmpty(item.Icon)) { if (string.IsNullOrEmpty(item.Text)) { // We assumed the default toolbar icon is "naviframe/drawer" if there are no icon and text. button.Style = StyleDefaultToolbarIcon; } else { if (position == ToolbarButtonPosition.Right) { button.Style = StyleRightToolbarButton; } else { button.Style = StyleLeftToolBarButton; } } } else { Native.Image iconImage = new Native.Image(Forms.NativeParent); var task = iconImage.LoadFromImageSourceAsync(item.Icon); button.Image = iconImage; } return(button); }
public ToolbarImageButton(Context context, ToolbarItem item) : base(context) { if (item == null) { throw new ArgumentNullException("item", "you should specify a ToolbarItem"); } Item = item; Enabled = Controller.IsEnabled; Bitmap bitmap; bitmap = Context.Resources.GetBitmap(Item.Icon); SetImageBitmap(bitmap); SetBackgroundColor(new Color(0, 0, 0, 0).ToAndroid()); Click += (sender, e) => Controller.Activate(); bitmap.Dispose(); Item.PropertyChanged += HandlePropertyChanged; }
void UpdateToolbarItems() { if (_page.ApplicationBar == null) { _page.ApplicationBar = new ApplicationBar(); } ToolbarItem[] items = _tracker.ToolbarItems.ToArray(); MasterDetailPage masterDetail = _tracker.Target.Descendants().Prepend(_tracker.Target).OfType <MasterDetailPage>().FirstOrDefault(); TaggedAppBarButton oldMasterDetailButton = _page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().FirstOrDefault(b => b.Tag is MasterDetailPage && b.Tag != masterDetail); if (oldMasterDetailButton != null) { _page.ApplicationBar.Buttons.Remove(oldMasterDetailButton); } if (masterDetail != null) { if (masterDetail.ShouldShowToolbarButton()) { if (_page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().All(b => b.Tag != masterDetail)) { var button = new TaggedAppBarButton { IconUri = new Uri(masterDetail.Master.Icon ?? "ApplicationIcon.jpg", UriKind.Relative), Text = masterDetail.Master.Title, IsEnabled = true, Tag = masterDetail }; button.Click += (sender, args) => { var masterDetailRenderer = GetRenderer(masterDetail) as MasterDetailRenderer; if (masterDetailRenderer != null) { masterDetailRenderer.Toggle(); } }; _page.ApplicationBar.Buttons.Add(button); } } } var buttonsToAdd = new List <TaggedAppBarButton>(); foreach (ToolbarItem item in items.Where(i => i.Order != ToolbarItemOrder.Secondary)) { IMenuItemController controller = item; if (_page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().Any(b => b.Tag == item)) { continue; } var button = new TaggedAppBarButton { IconUri = new Uri(item.Icon ?? "ApplicationIcon.jpg", UriKind.Relative), Text = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : (string)item.Icon ?? "ApplicationIcon.jpg", IsEnabled = controller.IsEnabled, Tag = item }; button.Click += (sender, args) => controller.Activate(); buttonsToAdd.Add(button); } var menuItemsToAdd = new List <TaggedAppBarMenuItem>(); foreach (ToolbarItem item in items.Where(i => i.Order == ToolbarItemOrder.Secondary)) { if (_page.ApplicationBar.MenuItems.OfType <TaggedAppBarMenuItem>().Any(b => b.Tag == item)) { continue; } var button = new TaggedAppBarMenuItem { Text = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : (string)item.Icon ?? "MenuItem", IsEnabled = true, Tag = item }; button.Click += (sender, args) => ((IMenuItemController)item).Activate(); menuItemsToAdd.Add(button); } TaggedAppBarButton[] deadButtons = _page.ApplicationBar.Buttons.OfType <TaggedAppBarButton>().Where(b => b.Tag is ToolbarItem && !items.Contains(b.Tag)).ToArray(); TaggedAppBarMenuItem[] deadMenuItems = _page.ApplicationBar.MenuItems.OfType <TaggedAppBarMenuItem>().Where(b => b.Tag is ToolbarItem && !items.Contains(b.Tag)).ToArray(); // we must remove the dead buttons before adding the new ones so we don't accidentally go over the limit during the transition foreach (TaggedAppBarButton deadButton in deadButtons) { deadButton.Dispose(); _page.ApplicationBar.Buttons.Remove(deadButton); } foreach (TaggedAppBarMenuItem deadMenuItem in deadMenuItems) { _page.ApplicationBar.MenuItems.Remove(deadMenuItem); } // fixme, insert in order foreach (TaggedAppBarButton newButton in buttonsToAdd) { _page.ApplicationBar.Buttons.Add(newButton); } foreach (TaggedAppBarMenuItem newMenuItem in menuItemsToAdd) { _page.ApplicationBar.MenuItems.Add(newMenuItem); } _page.ApplicationBar.IsVisible = _page.ApplicationBar.Buttons.Count > 0 || _page.ApplicationBar.MenuItems.Count > 0; }