我是Xamarin的新员工,我正在制作一个应用程序,在屏幕1上我显示了一个汽车制造商列表,点击集合视图中代表的某个制造商时,我显示了他们的车辆列表,用户可以更改制造商名称及其缩写,并更新它,删除制造商,并添加新的车辆给他们。问题是当我添加新车时,它似乎没有添加到该制造商,它的ID似乎没有定义。我正在将id传递给新视图,但看起来我做得不对,因为当我关闭车辆制造商过滤器(注释if)项时,车辆将不会出现在列表中。MakeId == itemId)在ExecuteLoadItemsCommand中的VehicleMakeViewModel),我可以看到车辆被添加,但id不是我想要设置的,它是空的。我可能做错了什么或忽视的东西,所以请如果你能看到的问题,帮助我。在VehicleMakeViewModel中,我执行添加新车辆、删除和更新制造商的逻辑,在NewVehicleModelViewModel中,我允许用户输入新车辆的信息。VehicleMakeViewModel:
namespace App3.ViewModels
{
[QueryProperty(nameof(ItemId), nameof(ItemId))]
public class VehicleMakeDetailViewModel : BaseViewModel
{
private VehicleModel _selectedItem;
public ObservableCollection<VehicleModel> VehicleModels { get; }
public Command LoadVehicleModelsCommand { get; }
public Command AddVehicleModelCommand { get; }
public Command<VehicleModel> VehicleModelTapped { get; }
public Command DeleteVehicleMakeCommand { get; }
public Command UpdateVehicleMakeCommand { get; }
public VehicleMakeDetailViewModel()
{
Title = "Vehicles";
VehicleModels = new ObservableCollection<VehicleModel>();
LoadVehicleModelsCommand = new Command(async () => await ExecuteLoadItemsCommand());
VehicleModelTapped = new Command<VehicleModel>(OnVehicleModelSelected);
AddVehicleModelCommand = new Command(OnAddItem);
UpdateVehicleMakeCommand = new Command(UpdateItem);
DeleteVehicleMakeCommand = new Command(DeleteItem);
}
async Task ExecuteLoadItemsCommand()
{
IsBusy = true;
try
{
VehicleModels.Clear();
var items = await BaseVehicleModelDataStore.GetItemsAsync(true);
foreach (var item in items)
{
//if (item.MakeId == itemId)
VehicleModels.Add(item);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
public void OnAppearing()
{
IsBusy = true;
SelectedItem = null;
}
public VehicleModel SelectedItem
{
get => _selectedItem;
set
{
SetProperty(ref _selectedItem, value);
OnVehicleModelSelected(value);
}
}
async void OnVehicleModelSelected(VehicleModel item)
{
if (item == null)
return;
// This will push the ItemDetailPage onto the navigation stack
await Shell.Current.GoToAsync($"{nameof(VehicleModelDetailPage)}?{nameof(VehicleModelDetailViewModel.ItemId)}={item.Id}");
}
private async void DeleteItem(object obj)
{
try
{
foreach(var item in VehicleModels)
{
if (item.MakeId == Id)
{
await BaseVehicleModelDataStore.DeleteItemAsync(item.Id);
}
}
await BaseVehicleMakeDataStore.DeleteItemAsync(Id);
await Shell.Current.GoToAsync(nameof(VehicleMakesPage));
/*ItemId = "";
VehicleMakeAbrv = "";
VehicleMakeName = "";*/
//App.Current.MainPage = new VehicleMakesPage();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private async void UpdateItem(object obj)
{
try
{
await BaseVehicleMakeDataStore.UpdateItemAsync(new VehicleMake { Id= Id, Name = VehicleMakeName, Abrv=VehicleMakeAbrv });
foreach(var item in VehicleModels)
{
if (item.MakeId == Id)
{
await BaseVehicleModelDataStore.UpdateItemAsync(new VehicleModel { Id = item.Id, Name = item.Name, Abrv = item.Abrv, MakeId = item.MakeId });
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private async void OnAddItem(object obj)
{
await Shell.Current.GoToAsync($"{nameof(NewVehicleModelPage)}?{nameof(NewVehicleModelViewModel.ItemId)}={ItemId}");
}
private string itemId;
private string vehicleMakeName;
private string vehicleMakeAbrv;
public string Id { get; set; }
public string VehicleMakeName
{
get => vehicleMakeName;
set => SetProperty(ref vehicleMakeName, value);
}
public string VehicleMakeAbrv
{
get => vehicleMakeAbrv;
set => SetProperty(ref vehicleMakeAbrv, value);
}
public string ItemId
{
get
{
return itemId;
}
set
{
itemId = value;
LoadItemId(value);
}
}
public async void LoadItemId(string itemId)
{
try
{
var item = await BaseVehicleMakeDataStore.GetItemAsync(itemId);
Id = item.Id;
VehicleMakeName = item.Name;
VehicleMakeAbrv = item.Abrv;
Title = Id;
}
catch (Exception)
{
Debug.WriteLine("Failed to Load Item");
}
}
}
}
NewVehicleModelViewModel:
namespace App3.ViewModels
{
class NewVehicleModelViewModel: BaseViewModel
{
private string text;
private string description;
private string itemId;
public NewVehicleModelViewModel()
{
SaveCommand = new Command(OnSave, ValidateSave);
CancelCommand = new Command(OnCancel);
this.PropertyChanged +=
(_, __) => SaveCommand.ChangeCanExecute();
Text = ItemId;
Description = ItemId;
}
private bool ValidateSave()
{
return !String.IsNullOrWhiteSpace(text)
&& !String.IsNullOrWhiteSpace(description);
}
public string ItemId
{
get
{
return itemId;
}
set
{
itemId = value;
//LoadItemId(value);
}
}
public async void LoadItemId(string itemId)
{
try
{
var item = await BaseVehicleMakeDataStore.GetItemAsync(itemId);
ItemId = item.Id;
//VehicleMakeName = item.Name;
//VehicleMakeAbrv = item.Abrv;
}
catch (Exception)
{
Debug.WriteLine("Failed to Load Item");
}
}
public string Text
{
get => text;
set => SetProperty(ref text, value);
}
public string Description
{
get => description;
set => SetProperty(ref description, value);
}
public Command SaveCommand { get; }
public Command CancelCommand { get; }
private async void OnCancel()
{
// This will pop the current page off the navigation stack
await Shell.Current.GoToAsync("..");
}
private async void OnSave()
{
VehicleModel newItem = new VehicleModel()
{
Id = Guid.NewGuid().ToString(),
Name = Text,
Abrv = Description,
MakeId = ItemId
};
await BaseVehicleModelDataStore.AddItemAsync(newItem);
// This will pop the current page off the navigation stack
await Shell.Current.GoToAsync("..");
}
}
}
我在NewVehicleModelViewModel类之前缺少QueryProperty。所以类NewVehicleModelViewModel看起来像这样:
namespace App3.ViewModels
{
[QueryProperty(nameof(ItemId), nameof(ItemId))]
class NewVehicleModelViewModel: BaseViewModel
{
... //other things are the same..