Xamarin SelectedValue在RadioButton的CollectionView中返回null



我是Xamarin的新手,我还在了解它的要点。我想创建一个单选按钮的集合视图,但是每当我提交选择时返回null而不是假设的值。

下面是我的代码: <<p>视图/strong>
<StackLayout>
<CollectionView ItemsSource="{Binding choices}"
RadioButtonGroup.SelectedValue="{Binding choice}">
<CollectionView.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding .}" Value="{Binding .}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Submit" Command="{Binding submit}"/>
</StackLayout>

ViewModel

public ObservableRangeCollection<string> choices { get; set; }
public toTestVM()
{
choices = new ObservableRangeCollection<string>();
List<string> tempList = new List<string>() { "Cat", "Dog", "Bird", "Chicken", "Cow","Fish"};
choices.AddRange(tempList);
submit = new AsyncCommand(promptAnswer);
}
public ICommand submit { get; }
private string _choice;
public string choice
{
get => _choice;
set => SetProperty(ref _choice, value);
}
public async Task promptAnswer()
{
if (!String.IsNullOrEmpty(_choice))
{
await App.Current.MainPage.DisplayAlert("", _choice, "OK");
}
}
}

答案不提示每当我点击提交,因为_choice仍然为空,尽管有OnPropertyChange();

我将感激任何帮助或建议。谢谢你选择列表以单选按钮的形式显示得很好。但即使我选择一个并单击提交,答案仍然返回null。代码有问题吗>

但即使我选择一个并单击提交,答案仍然返回null。代码有问题吗>

这是因为在RadioButton的事件CheckedChanged和collectionview的事件SelectionChanged之间存在冲突。

可以为CollectionView创建一个DataTemplate

基于你的代码,我创建了一个演示。如果我选择一个项目,我可以得到Choice

请参考以下代码:

TestPage.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:app1109="clr-namespace:App1109"
x:Class="App1109.TestPage2"
x:Name="MyPageName"
>
<ContentPage.BindingContext>
<app1109:TestViewModel></app1109:TestViewModel>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<CollectionView ItemsSource="{Binding choices}" x:Name="collectionView"
SelectedItem="{Binding Choice}"
SelectionMode="Single"
>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" x:Name="Item" >
<RadioButton Content="{Binding Name}" IsChecked="{Binding IsSelected}" WidthRequest="160"  >
<RadioButton.Behaviors>
<app1109:EventToCommandBehavior 
EventName="CheckedChanged"     
Command="{Binding BindingContext.MyRadioCommand, Source={x:Reference Name=MyPageName}}" CommandParameter="{Binding .}"
/>
</RadioButton.Behaviors>
</RadioButton>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Submit" Command="{Binding submit}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

TestViewModel.cs

public class TestViewModel: INotifyPropertyChanged 
{
public ObservableCollection<ItemModel> choices { get; set; }
public TestViewModel()
{
List<ItemModel> tempList = new List<ItemModel>() { new ItemModel{ Name= "Cat" }, new ItemModel { Name = "Dog" } , new ItemModel { Name = "Bird" ,IsSelected = true} , new ItemModel { Name = "Chicken" }, new ItemModel { Name = "Cow" }, new ItemModel { Name = "Fish" } };
choices = new ObservableCollection<ItemModel>(tempList);
}
public ICommand submit => new Command(promptAnswer);
public ICommand MyRadioCommand => new Command<ItemModel>(changeStateMethod);
public async void changeStateMethod(ItemModel item) {
await App.Current.MainPage.DisplayAlert("", item.Name, "OK");
}
ItemModel _choice;
public ItemModel Choice
{
get => _choice;
set => SetProperty(ref _choice, value);
}
public async void promptAnswer()
{
if (Choice !=null)
{
await App.Current.MainPage.DisplayAlert("", Choice.Name, "OK");
}
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}

ItemModel.cs

public class ItemModel: INotifyPropertyChanged 
{
public string Name { get; set; }
private bool _isSelected;
public bool IsSelected
{
set { SetProperty(ref _isSelected, value); }
get { return _isSelected; }
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}

注意:

对于EventToCommandBehavior.cs,您可以在这里参考示例代码:https://github.com/xamarin/xamarin-forms-samples/tree/main/Behaviors/EventToCommandBehavior/EventToCommandBehavior/Behaviors

最新更新