我是 Xamarin 的新手,我仍在了解它的要點。我想制作一個單選按鈕的集合視圖,但是每當我提交選擇時,都會回傳 null 而不是假定的值。
這是我的代碼:
看法
<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>
視圖模型
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。代碼有問題>
uj5u.com熱心網友回復:
但即使我選擇一個并單擊提交,答案仍然回傳 null。代碼有問題>
CheckedChanged那是因為RadioButton 的事件SelectionChanged和collectionview的事件有沖突。
您可以為 CollectionView 創建一個DataTemplate。
根據您的代碼,我創建了一個演示。Choice如果我選擇一個專案,我可以得到。
請參考以下代碼:
測驗頁.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>
測驗視圖模型.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;
}
專案模型.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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/530272.html
標籤:C#xamarin
上一篇:在螢屏上繪制一個矩形
