void UpdateRightIcon(ToolbarItemEx itemEx, IMenuItem menuItem) { menuItem.SetVisible(true); menuItem.SetEnabled(itemEx.IsEnabled); if (string.IsNullOrEmpty(itemEx.Resource)) { return; } var image = SvgToBitmap.GetBitmap(itemEx.Resource, 24, 24); var icon = new BitmapDrawable(Context.Resources, image); menuItem.SetIcon(icon); if (!IsForeColorDefault && itemEx.IsEnabled) { menuItem.Icon.SetTint(ForeColor); } else if (!itemEx.IsEnabled) { menuItem.Icon.SetTint(ForeColor); menuItem.Icon.SetAlpha(80); } }
void UpdateLeftIcon(ToolbarItemEx itemEx, IMenuItem menuItem) { if (string.IsNullOrEmpty(itemEx.Resource)) { return; } var image = SvgToBitmap.GetBitmap(itemEx.Resource, 24, 24); var icon = new BitmapDrawable(Context.Resources, image); if (!IsForeColorDefault && itemEx.IsEnabled) { icon.SetTint(ForeColor); } else if (!itemEx.IsEnabled) { icon.SetTint(ForeColor); icon.SetAlpha(80); } //戻った時にこうしとかないと表示されない。BeginInvokeOnMainThreadだと失敗することがある。時間も250は必要。 Device.StartTimer(TimeSpan.FromMilliseconds(250), () => { _toolbar.NavigationIcon = icon; return(false); }); _navigationCustomListener?.Dispose(); _navigationCustomListener = new NavigationIconClickListener(itemEx.Command, itemEx.CommandParameter); _toolbar.SetNavigationOnClickListener(_navigationCustomListener); menuItem.SetVisible(false); }
void UpdateIcon(ToolbarItemEx itemEx, UIBarButtonItem nativeItem) { itemEx.PropertyChanged -= ItemEx_PropertyChanged; itemEx.PropertyChanged += ItemEx_PropertyChanged; if (!itemEx.IsVisible) { nativeItem.Image = null; nativeItem.Title = null; return; } if (string.IsNullOrEmpty(itemEx.Resource)) { return; } nativeItem.Image = SvgToUIImage.GetUIImage(itemEx.Resource, 20, 20); nativeItem.Title = null; nativeItem.Style = UIBarButtonItemStyle.Plain; nativeItem.Enabled = itemEx.IsEnabled; }
/// <summary> /// Creates system-style buttons if requested and hooks up CanExecuteChanged for the ToolbarItem command. /// (after writing this I noticed that the latter will be in the next version of Forms... oh well) /// </summary> /// <param name="toolbarItem"></param> /// <returns></returns> private UIBarButtonItem CreateUIBarButtonItem(ToolbarItemEx toolbarItem) { UIBarButtonSystemItem?systemItem = null; switch (toolbarItem.ToolbarItemType) { case ToolbarItemType.Done: systemItem = UIBarButtonSystemItem.Done; break; case ToolbarItemType.Add: systemItem = UIBarButtonSystemItem.Add; break; case ToolbarItemType.Refresh: systemItem = UIBarButtonSystemItem.Refresh; break; case ToolbarItemType.Search: systemItem = UIBarButtonSystemItem.Search; break; } var buttonItem = systemItem != null ? new UIBarButtonItem(systemItem.Value, (s, e) => toolbarItem.Activate()) : toolbarItem.ToUIBarButtonItem(); // Xamarin.Forms 1.3.2 adds support for CanExecute, so we only need this for // our custom system buttons // if (systemItem != null) { var command = toolbarItem.Command; EventHandler canExecuteHandler = (s, e) => UpdateButtonState(toolbarItem, buttonItem); if (command != null) { command.CanExecuteChanged += canExecuteHandler; } toolbarItem.PropertyChanged += (s, e) => { if (e.PropertyName == ToolbarItem.CommandProperty.PropertyName) { if (command != null) { command.CanExecuteChanged -= canExecuteHandler; } UpdateButtonState(toolbarItem, buttonItem); if (toolbarItem.Command != null) { toolbarItem.Command.CanExecuteChanged += canExecuteHandler; } command = toolbarItem.Command; } }; UpdateButtonState(toolbarItem, buttonItem); } return(buttonItem); }
private static void UpdateButtonState(ToolbarItemEx toolbarItem, UIBarButtonItem buttonItem) { buttonItem.Enabled = toolbarItem.Command?.CanExecute(toolbarItem.CommandParameter) ?? false; }