MAUI中RadioButton靠GroupName属性显式分组实现互斥,必须设相同非空字符串值;通过RadioButtonGroup.SelectedValue绑定Value属性管理选中状态,初始选中由SelectedValue匹配Value决定,CheckedChanged事件仅响应用户点击。

MAUI 中的 RadioButton 不像传统平台那样依赖容器分组(如 Android 的 RadioGroup 或 Qt 的父控件自动互斥),而是靠 GroupName 属性显式定义逻辑分组,同一组内自动互斥——这是最核心的使用前提。
多个 RadioButton 要实现单选,必须给它们设置相同的 GroupName 值。这个值是字符串,区分大小写,且不能为空。没有 GroupName 的 RadioButton 是独立的,不参与任何互斥逻辑。
RadioButtonGroup.GroupName="gender"
GroupName="gender",但分散写容易遗漏或不一致"gender" 和 "role",彼此完全隔离MAUI 不直接暴露“哪个 RadioButton 被选中”,而是通过 RadioButtonGroup.SelectedValue 统一管理。它会自动同步为当前被选中项的 Value 属性值(类型为 object,通常用 string)。
SelectedGender),并双向绑定到 RadioButtonGroup.SelectedValue
Value="Male"、Value="Female" 等,值要与绑定属性类型兼容SelectedGender 自动更新,无需手动监听事件默认选中不是靠 IsChecked="True"(该属性在 MAUI RadioButton 中只读,设了也没用),而是靠 让 SelectedValue 初始值匹配某个 RadioButton 的 Value。
[ObservableProperty] string selectedGender = "Male";
Value="Male",启动时就会自动高亮它虽然绑定 SelectedValue 已覆盖大部分场景,但有时仍需监听变化时机。MAUI 提供 CheckedChanged 事件,注意它只在用户主动点击时触发,程序赋值不会触发。
CheckedChanged="OnGenderChanged"
if (e.Value) { DisplayAlert("提示", $"你选了{((RadioButton)sender).Content}", "确定"); }
基本上就这些。关键就是 GroupName 统一分组、Value 对齐 SelectedValue、靠绑定驱动状态,不复杂但容易忽略 GroupName 这个显式门槛。
以上就是MAUI RadioButton怎么用 MAUI单选按钮使用教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号