You need to enable JavaScript to run this app. 如何在Xamarin.Forms共享项目中使用C#代码创建Radio Button
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin.Forms共享项目中使用C#代码创建Radio Button

Xamarin.Forms 共享项目C#代码创建RadioButton方法

完全可以,Xamarin.Forms 5.0及以上版本已内置原生RadioButton控件,创建逻辑和你提到的CheckBox写法完全一致。


基础创建代码

直接实例化RadioButton类即可:
RadioButton myRadio = new RadioButton();


常用配置示例

RadioButton需要通过GroupName属性实现同组选项互斥选中,完整的使用示例如下:

// 声明同组两个单选按钮
RadioButton option1 = new RadioButton
{
    Content = "选项1",
    GroupName = "OptionGroup",
    IsChecked = true // 默认选中
};

RadioButton option2 = new RadioButton
{
    Content = "选项2",
    GroupName = "OptionGroup"
};

// 将控件添加到当前页面的布局容器中,示例用StackLayout
stackLayout.Children.Add(option1);
stackLayout.Children.Add(option2);

版本说明

如果你的项目使用的Xamarin.Forms版本低于5.0,没有内置RadioButton控件,建议先升级Xamarin.Forms版本到5.0及以上即可直接使用上述写法。

内容的提问来源于stack exchange,提问作者Megatron213

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 15:36:03