【问题标题】:Reseting Radio Buttons in Xamarin Forms 5在 Xamarin Forms 5 中重置单选按钮
【发布时间】:2021-11-29 19:37:08
【问题描述】:

我正在尝试创建一个 Trivia 应用程序。我有一些实现单选按钮的非常基本的 Xamarin 表单代码。按钮工作正常(即选择已正确注册和处理)。问题是在回答第一个问题后,当显示第二个问题时,单选按钮选择仍然存在。有没有办法重置问题之间的单选按钮,使它们都未被选中?

这是基本的xaml代码和背后的代码:

Xaml:

                 <StackLayout Margin="20">
                     <RadioButton x:Name="Option0" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
                     <RadioButton x:Name="Option1" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
                     <RadioButton x:Name="Option2" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
                     <RadioButton x:Name="Option3" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
                 </StackLayout>

后面的代码:(注意:getQuestion() 具有显示下一个问题的逻辑,因此我没有在此处包含它,我怀疑需要在 // 执行某些操作以重置单选按钮,但我不确定怎么办)

    void RadioButton_CheckedChanged(System.Object sender, Xamarin.Forms.CheckedChangedEventArgs e)  // sender is of class System.Object and must be converted in the proper type
    {
        Debug.WriteLine("\nRadio Button Clicked");
        Debug.WriteLine("sender: " + sender);
        Debug.WriteLine("e: " + e);

        var s = sender;
        var radioEvent = e;

        // converts sender to Radio Button type
        var radioButton = (RadioButton)sender;
        // Once in Radio Button type now you can see and use content
        string contentString = radioButton.Content.ToString();
        Debug.WriteLine("contentString: " + contentString);

        if (radioButton.IsChecked == true)
        {

            if (contentString == Movie.Text)
            {
                DisplayAlert("Correct", "You're Good!", "OK");
                Debug.WriteLine("Correct Answer");             
                ans_correct = ans_correct + 1;
                // do something to reset the radio button
                getQuestion();

            }
            else
            {
                DisplayAlert("Whoops", "Wrong Agian!", "OK");
                Debug.WriteLine("Wrong Answer");
                ans_wrong = ans_wrong + 1;
            }
        }
    }

【问题讨论】:

    标签: xamarin.forms radio-button reset


    【解决方案1】:

    没关系 - 我想我只需发布问题即可找到答案。

    原来我需要做的只是把

    radioButton.IsChecked = false;
    

    我的评论在哪里

    // do something to reset the radio button
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-02
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多