xamarin - 复选框值/无 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>
解决方案
您正在使用 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)
{
}
}
推荐阅读
- mongodb - 在 MongoDB ReplicaSet 中,优先级为 0 的节点(隐藏、延迟等)是否必须与可以成为主节点的节点具有相同的 Oplog 大小?
- sql - DATEDIFFT、EOMONTH 和 DATE 格式
- mqtt - Hivemq java客户端重新连接身份验证用户名和密码不去它正在尝试使用用户名和密码重新连接
- python - 哪些情况会导致 vscode python 调试器运行缓慢或死锁?
- python - Python 3.7 中有哪些 AsyncMock 替代方案?
- ios - 如何从 iOS 应用程序发起拨出电话?
- operating-system - 我们可以在仅 gpu 的设备中运行操作系统吗?(不考虑权衡)
- javascript - 如何在 React 应用程序中使用包中的主题?
- javascript - 如何根据项的属性键和值,使用必须从另一个不同的数据池中获取的过滤条件来过滤数组项?
- servlets - Servlet双重初始化Websphere