微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Xamarin.Forms 中使用 RadioButton

如何解决在 Xamarin.Forms 中使用 RadioButton

我正在尝试在 Xamarin 表单中使用 RadioButton:

<?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>

cs 文件

public SetupGender()
{
    InitializeComponent();
}


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

private void Button_Clicked(object sender,EventArgs e)
{

}

当我运行这个时,我收到以下错误

system.invalidOperationException: '你的类、属性方法 正在尝试使用 ('RadioButton') 是 RadioButton 的一部分;使用 它,您必须通过致电选择加入 Forms.SetFlags("RadioButton_Experimental") 调用前 Forms.Init().'

我可以尝试修复什么?

解决方法

在 XF5.0 RadioButton 是一项实验性功能之前,要使用它,您必须完全按照错误消息所述,在调用 Init 之前设置 experimental flag

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

Jason 的回答是正确的,但您可以在 App.xaml.cs 中实施以下修复:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。