xabre / xamarin-forms-tab-badge

Xamarin Forms bindable Tab badges for iOS, Android, UWP, MacOS and WPF
MIT License
307 stars 75 forks source link

Binding Issues on Xamarin forms #85

Open mohammedmsadiq opened 4 years ago

mohammedmsadiq commented 4 years ago

i cant get this to work through binding. but static number works, see below my implementation

(xaml file) <local:Page1 Title="My Page" x:Name="Item1" plugin:TabBadge.BadgeText="{Binding Count}" plugin:TabBadge.BadgeColor="Red" plugin:TabBadge.BadgeTextColor="White" />

(ViewModel) public override void OnAppearing() { var result = await this.notificationService.GetNotificationCount(); this.Count = result.Count; base.OnAppearing(); }

    public string Count
    {
        get
        {
            return this.count;
        }
        set
        {
            this.SetProperty(ref this.count, value);
        }
    }
mohammedmsadiq commented 4 years ago

i also tried the implementation on your sample project and property is not detected any changes. see below;

public class MasterTabbedPageViewModel : ViewModelBase, INotifyPropertyChanged { readonly INotificationService notificationService;

    public MasterTabbedPageViewModel(INavigationService navigationService, INotificationService notificationService, IPageDialogService pageDialogService, IProfilePictureService profilePictureService, IDeviceService deviceService, IEventAggregator eventAggregator, IAnalyticsService analyticsService) : base(navigationService, pageDialogService, profilePictureService, deviceService, eventAggregator, analyticsService)
    {
        Title = "MasterTabbedPage";
        this.notificationService = notificationService;           
    }

    public override void OnAppearing()
    {
        this.ExecuteAsyncTask(async () =>
        {
            var item = await this.notificationService.GetNotificationCount();
            courseCount = item.NewCourseCount;
            taskCount = item.TaskCount;
        });
        base.OnAppearing();
    }

    private int courseCount;

    public string CourseCount => courseCount <= 0 ? string.Empty : courseCount.ToString();

    public int CourseCountValue
    {
        get => courseCount;
        set
        {
            if (courseCount == value)
                return;

            courseCount = value;
            RaisePropertyChanged(nameof(CourseCountValue));
            RaisePropertyChanged(nameof(CourseCount));
        }
    }

    private int taskCount;

    public string TaskCount => taskCount <= 0 ? string.Empty : taskCount.ToString();

    public int TaskCountValue
    {
        get => taskCount;
        set
        {
            if (taskCount == value)
                return;

            taskCount = value;
            RaisePropertyChanged(nameof(TaskCountValue));
            RaisePropertyChanged(nameof(TaskCount));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}
jmblargent commented 4 years ago

If you are binding to the TabbedPage ViewModel, you need to specify the source and path for the binding. In your first example, if your TabbedPage is named Tabs, then the binding would look like {Binding Source={x:Reference Tabs}, Path=BindingContext.Count}. If you don't set the source, then it is looking for Count in the binding context of Page1.