Listview 根据xamarin中的api响应在列表视图中填充数据
`单击一个按钮,我添加了一个条目和一个删除图标。根据api响应,我想获得车辆列表以及进一步的添加、删除和更新过程 我已经编写了代码,但是在车辆中显示api响应时,在for循环之后,我无法在CollectionsList中获得正确的数据。我能够逐个显示车辆的api响应,但删除时不会删除正确的数据 XAMLListview 根据xamarin中的api响应在列表视图中填充数据,listview,xamarin,Listview,Xamarin,`单击一个按钮,我添加了一个条目和一个删除图标。根据api响应,我想获得车辆列表以及进一步的添加、删除和更新过程 我已经编写了代码,但是在车辆中显示api响应时,在for循环之后,我无法在CollectionsList中获得正确的数据。我能够逐个显示车辆的api响应,但删除时不会删除正确的数据 XAML <StackLayout x:Name="vehicleListLayout" Margin="40,0"> <ListVi
<StackLayout x:Name="vehicleListLayout" Margin="40,0">
<ListView x:Name="MyList" SeparatorVisibility="None"
HasUnevenRows="True" ItemsSource="{Binding CollectionsList}"
Footer="">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="0" RowSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Entry
Text="{Binding VehicleNumber1}"
WidthRequest="180"
Margin="5" VerticalOptions="Start"/>
<ImageButton Margin="5" Command="{Binding
Path=BindingContext.DeleteCommand, Source={x:Reference
MyList}}"
CommandParameter="{Binding .}"
Grid.Column="1" Source="baseline_remove_black_24"
HorizontalOptions="Start"
HeightRequest="20" WidthRequest="25"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
只需像这样更改ViewModelClass
public class ProfileViewModel : INotifyPropertyChanged
{
public ProfileData profileData;
public ProfileData ProfileData { get; private set; }
public int Id { get; set; }
private ObservableCollection<Vehicles> _CollectionsList;
public ObservableCollection<Vehicles> CollectionsList
{
get { return _CollectionsList; }
set
{
_CollectionsList = value;
OnPropertyChanged();
}
}
string name;
public string ProfileName
{
get => name;
set
{
if (name == value)
return;
name = value;
OnPropertyChanged();
}
}
public ICommand AddCommand { get; }
public ICommand DeleteCommand { get; }
public ICommand SaveCommand { get; }
public ProfileViewModel()
{
CollectionsList = new ObservableCollection<Vehicles>();
loadProfileData();
AddCommand = new Command(AddItems);
DeleteCommand = new Command(OnDeleteTapped);
SaveCommand = new Command(OnSaveTapped);
}
async void loadProfileData()
{
try
{
profileData = await
ProfileManager.GetProfileDataAsync();
}
catch (Exception e)
{
Console.WriteLine("WebService:GetEventsDataAsync:Failed
to
get events");
}
if (profileData == null)
{
profileData = new ProfileData();
}
//Loads Profile Data
ProfileName = profileData.name;
if (profileData.vehicles != null)
{
for (int i = 0; i < profileData.vehicles.Count; i++)
{
Vehicles vehicles = new Vehicles();
vehicles.VehicleNumber1 = profileData.vehicles[i];
CollectionsList.Add(vehicles);
}
}
Id = profileData.Id;
OnPropertyChanged("ProfileData");
}
private void AddItems(object obj)
{
Vehicles vehicles = new Vehicles();
CollectionsList.Insert(0, vehicles);
}
private void OnDeleteTapped(object obj)
{
var content = obj as Vehicles;
CollectionsList.Remove(CollectionsList.Single(v => v.VehicleNumber1 == content.VehicleNumber1));
}
private async void OnSaveTapped(object obj)
{
profileData.Id = Id;
profileData.name = name;
if (profileData.vehicles != null)
{
for (int i = 0; i < CollectionsList.Count; i++)
{
profileData.vehicles[i] =
CollectionsList[i].VehicleNumber1;
}
}
if (ProfileManager.UpdateProfileToDBAsync(profileData))
{
await Application.Current.MainPage.DisplayAlert("",
"Your
Profile Information is updated", "Ok");
}
else
{
await Application.Current.MainPage.DisplayAlert("",
"failed", "Ok");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName =
null)
{
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
#endregion
}
公共类ProfileViewModel:INotifyPropertyChanged
{
公共档案数据;
公共配置文件数据{get;private set;}
公共int Id{get;set;}
私人可观察收集(U CollectionsList);;
公共可观测收集列表
{
获取{return\u CollectionsList;}
设置
{
_CollectionsList=值;
OnPropertyChanged();
}
}
字符串名;
公共字符串配置文件名
{
get=>name;
设置
{
如果(名称==值)
返回;
名称=值;
OnPropertyChanged();
}
}
公共ICommand AddCommand{get;}
公共ICommand DeleteCommand{get;}
公共ICommand SaveCommand{get;}
公共档案视图模型()
{
CollectionsList=新的ObservableCollection();
loadProfileData();
AddCommand=新命令(AddItems);
DeleteCommand=新命令(OnDeleteTapped);
SaveCommand=新命令(OnSaveTapped);
}
异步void loadProfileData()
{
尝试
{
profileData=wait
GetProfileDataAsync();
}
捕获(例外e)
{
Console.WriteLine(“WebService:GetEventsDataAsync:Failed
到
获取事件“);
}
if(profileData==null)
{
profileData=新的profileData();
}
//加载配置文件数据
ProfileName=profileData.name;
if(profileData.vehicles!=null)
{
对于(int i=0;iv.VehicleNumber1==content.VehicleNumber1));
}
私有异步void OnSaveTapped(对象obj)
{
profileData.Id=Id;
profileData.name=名称;
if(profileData.vehicles!=null)
{
对于(int i=0;i
ListView和条目的代码在哪里?您发布的代码还包含一个不在方法中的大块,因此C#无效。@Jason我在.xaml的列表视图中添加了条目file@Jason它在一个方法中编写了一大块代码,这个方法在构造函数中被调用,我遵循了这个链接,请阅读——没有人会观看youtube视频来确定你在做什么。您需要能够在文章正文中提供足够的详细信息,以便我们能够帮助您。@Jason in.xaml中我添加了项目源,我在ViewModel类中使用的名称与CollectionsList相同-我在删除函数中添加了,在模型类中添加了一些UniqueProperty,我是否需要在.xaml文件中添加任何内容,因为其崩溃引发的System.InvalidOperationException和Sequence包含多个匹配元素,我调试了它,并且content.SomeUniqueProperty为null。@SaumyaSaloni我使用SomeUniqueProperty作为示例。只需将其替换为在车辆中唯一的属性,如id、名称等。另一个错误是移动此行Vehicles Vehicles=new Vehicles();为了在您的for循环中遍历profileData.vehicles.i添加了x:Name=“DeleteList”,对于显示VehicleNumber n的条目,我使用v.DeleteList添加了您的代码,我得到了一个错误以及在我的模型中添加该属性的建议。我有两个模型,第一个模型有一个车辆列表--profileData-public list vehicles{get;set;}--和第二个VehicleNumber 1 Vehicles-公共字符串VehicleNumber 1{get;set;}-我在.xaml中绑定了文本作为条目text=“{Binding VehicleNumber 1}”
public class ProfileViewModel : INotifyPropertyChanged
{
public ProfileData profileData;
public ProfileData ProfileData { get; private set; }
public int Id { get; set; }
private ObservableCollection<Vehicles> _CollectionsList;
public ObservableCollection<Vehicles> CollectionsList
{
get { return _CollectionsList; }
set
{
_CollectionsList = value;
OnPropertyChanged();
}
}
string name;
public string ProfileName
{
get => name;
set
{
if (name == value)
return;
name = value;
OnPropertyChanged();
}
}
public ICommand AddCommand { get; }
public ICommand DeleteCommand { get; }
public ICommand SaveCommand { get; }
public ProfileViewModel()
{
CollectionsList = new ObservableCollection<Vehicles>();
loadProfileData();
AddCommand = new Command(AddItems);
DeleteCommand = new Command(OnDeleteTapped);
SaveCommand = new Command(OnSaveTapped);
}
async void loadProfileData()
{
try
{
profileData = await
ProfileManager.GetProfileDataAsync();
}
catch (Exception e)
{
Console.WriteLine("WebService:GetEventsDataAsync:Failed
to
get events");
}
if (profileData == null)
{
profileData = new ProfileData();
}
//Loads Profile Data
ProfileName = profileData.name;
if (profileData.vehicles != null)
{
for (int i = 0; i < profileData.vehicles.Count; i++)
{
Vehicles vehicles = new Vehicles();
vehicles.VehicleNumber1 = profileData.vehicles[i];
CollectionsList.Add(vehicles);
}
}
Id = profileData.Id;
OnPropertyChanged("ProfileData");
}
private void AddItems(object obj)
{
Vehicles vehicles = new Vehicles();
CollectionsList.Insert(0, vehicles);
}
private void OnDeleteTapped(object obj)
{
var content = obj as Vehicles;
CollectionsList.Remove(CollectionsList.Single(v => v.VehicleNumber1 == content.VehicleNumber1));
}
private async void OnSaveTapped(object obj)
{
profileData.Id = Id;
profileData.name = name;
if (profileData.vehicles != null)
{
for (int i = 0; i < CollectionsList.Count; i++)
{
profileData.vehicles[i] =
CollectionsList[i].VehicleNumber1;
}
}
if (ProfileManager.UpdateProfileToDBAsync(profileData))
{
await Application.Current.MainPage.DisplayAlert("",
"Your
Profile Information is updated", "Ok");
}
else
{
await Application.Current.MainPage.DisplayAlert("",
"failed", "Ok");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName =
null)
{
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
#endregion
}