首页 > 解决方案 > 复选框值/无 MVVM Xamarin

问题描述

我有一组复选框,我需要它们来更改值.. 如果选中一个复选框,则应取消选中另一个。不想实现 MVVM 的原因是这只是现有应用程序的新前端,我只需要在合并项目之前让这些工作。这是我所拥有的,但它不起作用,我得到空引用/System.NullReferenceException:'对象引用未设置为对象的实例。

我的代码中有这个

private void AdvancedCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
        {
            if (AdvancedCheckBox.IsChecked == true) //System.NullReferenceException: 'Object reference not set to an instance of an object.'
            {
                AllCheckBox.IsChecked = false;
                UpperIntermediateCheckBox.IsChecked = false;
                AdvancedCheckBox.IsChecked = false;

            }
            else
                AdvancedCheckBox.IsChecked = true;
        }

        private void UpperIntermediateCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
        {
            if (UpperIntermediateCheckBox.IsChecked == true)
            {
                AllCheckBox.IsChecked = false;
                UpperIntermediateCheckBox.IsChecked = false;
                AdvancedCheckBox.IsChecked = false;

            }
            else
                UpperIntermediateCheckBox.IsChecked = true;
        }

        private void IntermediateCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
        {
            CheckBox IntermediateCheckBox = sender as CheckBox;
            if (IntermediateCheckBox.IsChecked == false)
            {
                AllCheckBox.IsChecked = false;
                UpperIntermediateCheckBox.IsChecked = false;
                AdvancedCheckBox.IsChecked = false ;

            }
            else
                IntermediateCheckBox.IsChecked = true ;
        }

        private void AllCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
        {
            CheckBox AllCheckBox = sender as CheckBox;
            if (AllCheckBox.IsChecked == true) System.NullReferenceException: 'Object reference not set to an instance of an object.'
            {

                UpperIntermediateCheckBox.IsChecked = false;
                AdvancedCheckBox.IsChecked = false;
                IntermediateCheckBox.IsChecked = false;
            }
            else
                AllCheckBox.IsChecked = true;
        }

和我的 XAML

<StackLayout Grid.Row="0"
                            Orientation="Horizontal"
                            Spacing="10"
                            HorizontalOptions="Start">
                    <grial:Checkbox x:Name="AllCheckBox"
                            IsCheckedChanged ="AllCheckBox_IsCheckedChanged"
                            HorizontalOptions="Start">
                        <Label
                            Margin="10,0"
                            FontSize="19"
                            Text="{ grial:Translate A_LabelAllArticles}"
                            VerticalOptions="Center"
                            TextColor="{ DynamicResource AccentColor }" />
                    </grial:Checkbox>

                </StackLayout>
                <StackLayout  Grid.Row="1"
                            Orientation="Horizontal"
                            Spacing="10"
                            HorizontalOptions="Start">
                    <grial:Checkbox x:Name="BeginnerCheckBox"
                        IsChecked="false"
                        HorizontalOptions="Start">
                        <Label
                            FontSize="19"
                             Margin="10,0"
                            Text="{ grial:Translate A_LabelBeginnerArticles}"
                            VerticalOptions="Center"
                            TextColor="{ DynamicResource AccentColor }" />
                    </grial:Checkbox>

                </StackLayout>
                <StackLayout  Grid.Row="2"
                            Orientation="Horizontal"
                            Spacing="10"
                            HorizontalOptions="Start">
                    <grial:Checkbox x:Name="IntermediateCheckBox"
                            IsCheckedChanged="IntermediateCheckBox_IsCheckedChanged"
                            HorizontalOptions="Start">
                        <Label
                            FontSize="19"
                            Margin="10,0"
                            Text="{ grial:Translate A_LabelIntermediateArticles}"
                            VerticalOptions="Center"
                            TextColor="{ DynamicResource AccentColor }" />
                    </grial:Checkbox>

                </StackLayout>
                <StackLayout  Grid.Row="3"
                            Orientation="Horizontal"
                            Spacing="10"
                            HorizontalOptions="Start">
                    <grial:Checkbox x:Name="UpperIntermediateCheckBox"
                            IsCheckedChanged="UpperIntermediateCheckBox_IsCheckedChanged"
                            HorizontalOptions="Start">
                        <Label
                            FontSize="19"
                            Margin="10,0"
                            Text="{ grial:Translate A_LabelUpperIntermediateArticles}"
                            VerticalOptions="Center"
                            TextColor="{ DynamicResource AccentColor }" />
                    </grial:Checkbox>

                </StackLayout>
                <StackLayout  Grid.Row="4"
                            Orientation="Horizontal"
                            Spacing="10"
                            HorizontalOptions="Start">
                    <grial:Checkbox x:Name="AdvancedCheckBox"
                        IsCheckedChanged="AdvancedCheckBox_IsCheckedChanged"
                            HorizontalOptions="Start">
                        <Label
                            FontSize="19"
                            Margin="10,0"
                            Text="{ grial:Translate A_LabelAdvancedArticles}"
                            VerticalOptions="Center"
                            TextColor="{ DynamicResource AccentColor }" />
                    </grial:Checkbox>

标签: xamarinxamarin.forms

解决方案


您正在使用 Grial UIKit 中的复选框,但将发件人定义为本机 Xamarin.Forms CheckBox,使用 Grial CheckBox 作为发件人的铸造类

//This will make every checkbox to checked or unchecked depending of the state of the clicked one
private void AllCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
{
    CheckBox ClickedCheckbox = sender as UXDivers.Grial.Checkbox;
    ChangeAllCheckboxesTo(ClickedCheckbox.IsChecked != null ? ClickedCheckbox.IsChecked : false);
}

//This will make every checkbox to false if you select one
private void AnyCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e)
{
    CheckBox ClickedCheckbox = sender as UXDivers.Grial.Checkbox;
    bool WasSelected = ClickedCheckbox.IsChecked != null ? ClickedCheckbox.IsChecked != null : false;
    ChangeAllCheckboxesTo(false);
    if(!WasSelected)
        ClickedCheckbox.IsChecked = true;
}

public void ChangeAllCheckboxesTo(bool value)
{
    try
    {
        UpperIntermediateCheckBox.IsChecked = value;
        AdvancedCheckBox.IsChecked = value;
        IntermediateCheckBox.IsChecked = value;
    }
    catch(Exception ex)
    {
    }
}

推荐阅读