Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

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

$
0
0

Hi,

I am creating an application in that I need to implement badge counter above the icon in toolbaritem. I am creating dependancy service for ios and android. I get reference of this url. https://www.xamboy.com/2018/03/08/adding-badge-to-toolbaritem-in-xamarin-forms/
Atter implement this I am getting perfect result in android but when I try to run it over ios device than able to use that. In that var rightButtonItems = vc?.ParentViewController?.NavigationItem?.RightBarButtonItems;
I got this always null so I am not able to use and set badge counter above the application.
I am using .net standard class library.
I get my same issue over here: https://xamarin.github.io/bugzilla-archives/22/22621/bug.html

Let I share my code.

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().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?


Viewing all articles
Browse latest Browse all 89864

Trending Articles