Reputation: 3473
I am trying to create Xamarin form which has an Android rating bar and iOS slider. Below is the XAML file I am using. I can see the Label but not able to see the rating bar when I try to run in Android device. Please help.
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ios="clr-namespace:UIKit;assembly=Xamarin.iOS;targetPlatform=iOS"
xmlns:android="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
xmlns:androidForms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
mc:Ignorable="d"
x:Class="XamrineTest.Page1">
<StackLayout BackgroundColor="Green" >
<Label Text="Welcome to Page1!" BackgroundColor="Gray"/>
<ios:UIDatePicker />
<ios:UISlider MaxValue="10" Value="{Binding SlideValue}" />
<ios:UIStepper />
<ios:UISwitch />
<android:RatingBar BackgroundColor="Lavender" HorizontalOptions="Center"
Scale="0.4"
StarCount="5" Step="0.5" Margin="0" SelectedColor="Orange"
HeightRequest="40" UnSelectedColor="LightGray" Rate="{Binding Rating,Mode=TwoWay}">
</android:RatingBar>
</StackLayout>
</ContentPage>
Upvotes: 0
Views: 519
Reputation: 13803
I think there are several problem in your code:
1.the namespace you used is not right, just change to this:
<androidWidget:RatingBar x:Arguments="{x:Static androidLocal:MainActivity.Instance}" >
</androidWidget:RatingBar>
And using following namespacing:
xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
2.we also need the following code to init androidWidget:RatingBar
x:Arguments="{x:Static androidLocal:MainActivity.Instance}"
define the Instance
in MainActivity
of android code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static MainActivity Instance { get; private set; } // define Instance
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
Instance = this;// init Instance
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
For more details, you can check:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/native-views/xaml
And this link also includes some useful samples, you can refer to it.
Update:
Yes, you can also use x:Arguments="{x:Static androidForms:Forms.Context}"
,
and the following code works properly.
<android:RatingBar x:Arguments="{x:Static androidForms:Forms.Context}" NumStars="5" StepSize="1.0" Rating="{Binding Rating,Mode=TwoWay}" />
Note:
The properties as follows you used in RatingBar does not exist.
BackgroundColor="Lavender" HorizontalOptions="Center"
Scale="0.4"
StarCount="5" Step="0.5" Margin="0" SelectedColor="Orange"
HeightRequest="40" UnSelectedColor="LightGray" Rate="{Binding Rating,Mode=TwoWay}"
Upvotes: 1