How to set badge counter above the toolbaritem in Xamarin.Forms?

偶尔善良 提交于 2019-12-11 06:17:57

问题


I am creating an application where I need to implement a badge counter above the icon in ToolbarItem. I am creating a dependancy service for IOS and Android. I reference this URL.

After implementing this I am getting a perfect result in Android, but when I try to run it on an IOS device I'm not able to use that. In that

var rightButtonItems = vc?.ParentViewController?.NavigationItem?.RightBarButtonItems;

always returns null so I am not able to use and set badge counter above the application.

I am using .Net standard class library. I have the same issue as here.

My code:

Badge.xaml

<ContentPage.ToolbarItems>
    <ToolbarItem Icon="notification" Name="MenuItem1" Order="Primary" Priority="0" Text="Notification" Clicked="ToolbarItem_Clicked"/>
</ContentPage.ToolbarItems>

Badge.xaml.cs

private void ToolbarItem_Clicked(object sender, EventArgs e)
{
    Navigation.PushAsync(new Notification());
}
private void ContentPage_Appearing(object sender, EventArgs e)
{
    base.OnAppearing();
    ViewModel.AppearingCommand?.Execute(null);
    if (ToolbarItems.Count > 0)
        DependencyService.Get<IToolbarItemBadgeService>().SetBadge(this, ToolbarItems.First(), "4", Color.Red, Color.White);
}

IToolbarItemBadgeService.cs(In shared project)

public interface IToolbarItemBadgeService
{
    void SetBadge(Page page, ToolbarItem item, string value, Color backgroundColor, Color textColor);
}

ToolbarItemBadgeService.cs(In iOS project)

[assembly: Dependency(typeof(ToolbarItemBadgeService))]
namespace CustomerServiceApp.iOS
{
    public class ToolbarItemBadgeService :  IToolbarItemBadgeService
    {

        public void SetBadge(Page page, ToolbarItem item, string value, Color backgroundColor, Color textColor)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                var renderer = Platform.GetRenderer(page);
                if (renderer == null)
                {
                    renderer = Platform.CreateRenderer(page);
                    Platform.SetRenderer(page, renderer);
                }
                var vc = renderer.ViewController;
                var rightButtonItems = vc?.ParentViewController?.NavigationItem?.RightBarButtonItems;//Here i get null in rightButtonItems
                // If we can't find the button where it typically is check the child view controllers
                // as this is where MasterDetailPages are kept
                if (rightButtonItems == null && vc.ChildViewControllerForHomeIndicatorAutoHidden != null)//vc.ChildViewControllerForHomeIndicatorAutoHidden is also return null
                    foreach (var uiObject in vc.ChildViewControllerForHomeIndicatorAutoHidden)
                    {
                        string uiObjectType = uiObject.GetType().ToString();
                        if (uiObjectType.Contains("FormsNav"))
                        {
                            UIKit.UINavigationBar navobj = (UIKit.UINavigationBar)uiObject;

                            if (navobj.Items != null)
                                foreach (UIKit.UINavigationItem navitem in navobj.Items)
                                {
                                    if (navitem.RightBarButtonItems != null)
                                    {
                                        rightButtonItems = navitem.RightBarButtonItems;
                                        break;
                                    }
                                }
                        }
                    }
                var idx = page.ToolbarItems.IndexOf(item);
                if (rightButtonItems != null && rightButtonItems.Length > idx)
                {
                    var barItem = rightButtonItems[idx];
                    if (barItem != null)
                    {
                        barItem.UpdateBadge(value, backgroundColor.ToUIColor(), textColor.ToUIColor());
                    }
                }
            });
        }
    }
}

Can anyone look into this and suggest me what should I have to do in that?

来源:https://stackoverflow.com/questions/56766023/how-to-set-badge-counter-above-the-toolbaritem-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!