Bryan
Bryan

Reputation: 3541

Use RadioButton in Xamarin.Forms

I'm trying to use RadioButton in Xamarin forms:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Prototype.Views.SetupGender">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Im a"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />

            <RadioButton Text="Male" />
            <RadioButton Text="Female"/>
            <Button Text="Next" WidthRequest="100" TextColor="White" BackgroundColor="#0077BE" Clicked="Button_Clicked"></Button>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The cs-file:

public SetupGender()
{
    InitializeComponent();
}


void OnGenderChange(object sender, CheckedChangedEventArgs e)
{
    if(e.Value)
    {
        // Is checked
        
    }
}

private void Button_Clicked(object sender, EventArgs e)
{

}

When I run this, I get the following error:

System.InvalidOperationException: 'The class, property, or method you are attempting to use ('RadioButton') is part of RadioButton; to use it, you must opt-in by calling Forms.SetFlags("RadioButton_Experimental") before calling Forms.Init().'

What can I try to fix it?

Upvotes: 0

Views: 1631

Answers (2)

Savage
Savage

Reputation: 2349

Jason's answer is correct, but you can implement the following fix in App.xaml.cs:

public partial class App : Application
{
    public App()
    {
        Device.SetFlags(new[] { "RadioButton_Experimental" });
        InitializeComponent();
        // ...
    }

Upvotes: 3

Jason
Jason

Reputation: 89117

before XF5.0 RadioButton was an experimental feature, and to use it you have to do exactly what the error message says, set the experimental flag before you call Init

Xamarin.Forms.Forms.SetFlags("RadioButton_Experimental");

Upvotes: 3

Related Questions