RatingBar Control- is a cross-platform plugin used for selecting the rating.
How To Use
Available on NuGet: https://www.nuget.org/packages/RatingBarControl/1.0.0
Install this Plugin in your Xamarin Form Project.
Install this Plugin in your Xamarin Form Project.
Namespace: xmlns:ratingbarcontrol="clr-namespace:RatingBarControl;assembly=RatingBarControl"
Implementation Example
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ratingbarcontrol="clr-namespace:RatingBarControl;assembly=RatingBarControl" x:Class="TestApp.MainPage"> <StackLayout Margin="0,10,0,0" HorizontalOptions="Center" > <Label Text="{Binding Source={x:Reference rtControl},Path=SelectedStarValue,StringFormat='Seleced Star: {0}'}" /> <ratingbarcontrol:RatingBar StarHeightRequest="35" StarWidthRequest="35" EmptyStarImage="emptystar" FillStarImage="fillstar" x:Name="rtControl"/> </StackLayout> </ContentPage>
RatingBar Control Property
- StarHeightRequest: Used to set the Height of the Image.
- StarWidthRequest: Used to set the Width of the Image.
- EmptyStarImage: Specify the image name of the empty star.
- FillStarImage: Specify the image name of the filled star.
- SelectedStarCommand: Use this command to get the selected star value in ViewModel.
- SelectedStarValue: Used to set default selected star value.
- FlowDirection: Used to set the Rating Bar Control Flow Direction, either Left to Right or Right to left.
- Spacing: Used to set the Spacing between stars.
How to display the selected star rating
Write the Following code to display the selected star rating
<Label Text="{Binding Source={x:Reference rtControl},Path=SelectedStarValue,StringFormat='Seleced Star: {0}'}" / <ratingbarcontrol:RatingBar StarHeightRequest="35" StarWidthRequest="35" EmptyStarImage="emptystar" FillStarImage="fillstar" x:Name="rtControl"/>
How to get the selected rating
SelecedStarCommand: Use this command to get the selected star value in ViewModel.
Design Side:
<Label Text="{Binding SelectedStarValue,StringFormat='Selected Star is : {0}'}" /> <ratingbarcontrol:RatingBar StarHeightRequest="35" StarWidthRequest="35" EmptyStarImage="emptystar" FillStarImage="fillstar" SelectedStarCommand="{Binding SelectedStarCommand}" x:Name="rtControl"/>
Code Behind (ViewModel):
private int _selectedStarValue; public int SelectedStarValue { get => _selectedStarValue; set => SetProperty(ref _selectedStarValue, value); } public ICommand SelectedStarCommand => new Command<int>((selectedStarValue) => { SelectedStarValue = selectedStarValue; });
What a great job!!! Well done!
ReplyDeleteYeah Thankyou
DeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
DeleteYeah thank you bro...
ReplyDeleteAmazing Blog.. Thanks for sharing great information about Xamarin.
ReplyDeleteHire Xamarin Developer
Thank you..
DeleteHi,
ReplyDeleteThanks, that I've found this post.
I'm trying to implement it in Android.
i set the lines:
private static string emptyStarImage = "emptyStarImage.png"; // string.Empty;
private static string fillStarImage = "fillStarImage.png"; // string.Empty;
But I'm not seeing an empty / filled star image.
Anything I'm missing?
Hi, Can you check the image path.
DeleteI think your empty star image path or filename might be wrong.
You can temporary check image by setting image on design.
This comment has been removed by the author.
DeleteRattingBarPage.Xaml
Delete"fillstar.png" and "emptystar.png" files paste in drawable folder.
Hi,
ReplyDeleteGreat code, thank you.
I want to add mutliple rattingbar in the same page. Just the lastone is rendered.
Do you know why ?
Thank you, If you want to add multiple ratting bar, then same ratting bar control either you can use multiple time on Page or you can use ListView.
DeleteHi Cedric, I have Fixed the issue of multiple Rattingbar. Now it will display multiple RattingBar and same code updated on my blog and github repo too.
Deletehttps://github.com/mistrypragnesh40/RattingBar-master.git
https://github.com/mistrypragnesh40/RattingBar-master.git
hi i'm getting error in the `ImageButtonView` in the below code:
ReplyDeleteprivate static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(
propertyName: "CommandParameter",
returnType: typeof(object),
declaringType: typeof(ImageButtonView),
propertyChanged: CommandParameterPropertyChanged
);
https://xamarincodingtutorial.blogspot.com/2019/05/step-to-create-imagebutton-with-text.html
DeleteThere was a minor mistake in this code. I have updated this blog content. Here declaring type will be RattingBar instead of ImageButtonView sorry for inconvenience.
DeleteImageButtonView give error what is this please give me project
ReplyDeleteUse Declaring Type as RattingBar Instead of ImageButtonView
Deleteplease add image button view code to complete
ReplyDeleteHi i have added Project here please go through it.
Deletehttps://github.com/mistrypragnesh40/RattingBar
Thank you.
when i am entering multiple rating bar its showing only last why?
ReplyDeleteHello Chanda, I have Fixed the issue of multiple Ratting bar now showing and same code updated on my blog and github repo too.
Deletehttps://github.com/mistrypragnesh40/RattingBar-master.git
Hi, very nice post. I want to ask what is rattingBarCommand doing inside RattingBar control in XAML?
ReplyDeleteYeah Thank you, and rattingBarCommand command will executed on the Selection of Each Star. if you want to perform any action on the selection of each star then you can use this command. I forgot to initialize ratting bar command on RattingBarViewModal but you can initialize it.
ReplyDeleteThis is so awesome, thank you
ReplyDelete