复选框值/无MVVM Xamarin

复选框值/无MVVM Xamarin,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一组复选框,需要它们来更改值。。如果一个被选中,另一个应该被取消选中。不想实现MVVM的原因是,这只是现有应用程序的新前端,我只需要在合并项目之前让它们工作。这就是我拥有的,但它不起作用,我得到null reference/System.NullReferenceException:“对象引用未设置为对象的实例。” 我的密码里有这个 private void AdvancedCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEv

我有一组复选框,需要它们来更改值。。如果一个被选中,另一个应该被取消选中。不想实现MVVM的原因是,这只是现有应用程序的新前端,我只需要在合并项目之前让它们工作。这就是我拥有的,但它不起作用,我得到null reference/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>

您使用的是Grial UIKit中的复选框,但将发送者定义为本机Xamarin。如果是Forms复选框,请使用Grial复选框作为发送者的强制类型

//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)
    {
    }
}

您的前三个方法在IF中有相同的语句-这似乎不正确是的,我尝试了不同的方法我添加了一个anwser,但我没有太多的上下文说明是什么导致了nullexception,您能将该信息添加到问题中吗?如果需要,我很乐意更新我的anwserto@Ricardo迪亚斯·莫瑞斯你好,这看起来很棒。但我也有同样的例外。我想我遗漏了什么。这是下划线和(ClickedCheckbox.IsChecked!=null?这是输出结果,结果始终为null,因为它从不等于类型的nullbool@RicardoDias Morais bool wassected=ClickedCheckbox.IsChecked!=null?ClickedCheckbox.IsChecked!=null:false;System.NullReferenceException:“对象引用未设置为对象的实例。”