【问题标题】:Xamarin RadioButton IsChecked is reset by other RadioButtonGroup after Appearing finishXamarin RadioButton IsChecked 在出现完成后由其他 RadioButtonGroup 重置
【发布时间】:2021-12-12 11:42:03
【问题描述】:

我在 Xamarin.Form 上创建动态 RadioButton 时遇到了一些问题,但无法使用构造函数,因为当我在 OnAppearing() 的 3 个 RadioButtonGroups 中创建动态 RadioButton 并在每个组中设置 IsChecked=true 的 1 个 RadioButton 但出现时完成所有 RadioButton。 3组IsChecked被重置,只有1个按钮被选中。我不知道原因。谁能帮我解决问题? 谢谢。

这是我的代码 [C#]

    public class Variant {
        public string variant_name { get; set; } 
        public int show_col { get; set; } 
        public bool default_value { get; set; } 
    }


    protected override void OnAppearing()
    {
        base.OnAppearing();
        GenerateChoice();
    }

    public void GenerateChoice()
    {
        try
        {
            foreach (var variant in variants)
            {
                if (null == variant)
                    continue;

                GenerateRadioButton(variant);
            }
            return;
        }
        catch (Exception ex)
        {
            return;
        }
    }
    public void GenerateRadioButton(Variant variant)
    {
        try
        {
            RadioButton rdChoice = new RadioButton
            {
                GroupName = "Radio" + variant.show_col,
                Content = variant.variant_name,                    
                Value= variant.variant_key,
                IsEnabled = true                    
            };
            rdChoice.CheckedChanged += (sender, e) =>
            {
                // Perform required operation after examining e.Value
                var ctl = (RadioButton)sender;
                if (ctl.IsChecked)
                    AddVariant(variant.variant_key);
                else
                    RemoveVariant(variant.variant_key);
            };
            if (variant.default_value)
            {
                rdChoice.IsChecked = true;
            }

            // set to RadioButtonGroup
            int col = variant.show_col;
            if (0 == col)
                groupRadio1.Children.Add(rdChoice);
            else if (1 == col)
                groupRadio2.Children.Add(rdChoice);
            else if (2 == col)
                groupRadio3.Children.Add(rdChoice);

            return;
        }
        catch (Exception ex)
        {
            return;
        }
    }

[XAML]

        <Grid x:Name="groupRadio"
            ColumnDefinitions="*,*,*"
            IsVisible="{Binding show_radio_type}"
            >                
            <!-- column #1 -->
            <StackLayout x:Name="groupRadio1"
                RadioButtonGroup.GroupName="Radio1"  
                Grid.Column="0"
                Grid.Row="0"
                Orientation="Vertical"         
                >
            </StackLayout>
            <!-- column #2 -->
            <StackLayout x:Name="groupRadio2"
                RadioButtonGroup.GroupName="Radio2"  
                Grid.Column="1"
                Grid.Row="0"
                Orientation="Vertical"         
                >
            </StackLayout>
            <!-- column #3 -->
            <StackLayout x:Name="groupRadio3"
                RadioButtonGroup.GroupName="Radio3"  
                Grid.Column="2"
                Grid.Row="0"
                Orientation="Vertical"         
                >
            </StackLayout>
        </Grid>

有 3 组 RadioButton 在组 #1 中检查为“Packed”,在组 #2 中检查“Oil”,在组 #3 中检查“Inland”。 我在创建过程中运行调试,它们被正确检查,但是当 OnAppearing() 完成时,我得到了取消选中“Packed”和“Oil”的事件

【问题讨论】:

  • 你希望你的单选按钮如何工作。
  • @Prabhat Kumar:感谢您的回复,我已针对您的问题编辑了我的帖子。

标签: xamarin dynamic radio-button


【解决方案1】:

根据您的代码,所有RadioButton 都具有相同的GroupName 属性。

GroupName,字符串类型,它定义了指定哪个 RadioButton 控件是互斥的。该物业有一个 默认值为 null。

这就是为什么即使你都检查过,只有最后一个看起来检查过。因为它们是相互排斥的。

您只需在创建组件时添加该属性

        RadioButton rdChoice = new RadioButton
        {
            Content = variant.variant_name,                    
            Value= variant.variant_key,
            IsEnabled = true,
            GroupName = variant.groupIdentifierOrSomethingLikethat, 
            IsChecked = variant.default_value,                   
        };

        rdChoice.CheckedChanged += (sender, e) =>
        { 
             ....
        };
        //if (variant.default_value)
        //    rdChoice.IsChecked = true;

【讨论】:

  • 感谢您的解决方案,我已尝试但问题仍然存在。请查看我已更改的帖子作为您的解决方案。
  • 这很奇怪。你的代码看起来不错。我编辑了答案,尝试在创建组件时设置IsChecked 属性。
  • 我已经尝试过,但输出相同,先生,Unchecked 事件仍然发送给其他人。页面完全呈现后是否有触发事件?
  • 如果你在base.OnAppearing()之前调用GenerateChoice会怎样?
  • 我试过了,一样
猜你喜欢
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多