I'm having trouble copying data from one ObservableCollection to another. I have an api call GetItemsAsync from http that puts the response into a model called ShipList.cs. Inside of ShipList.cs there is ShipCatalog[] ships. I have created a second model called HangarList.cs with HangarCatalog[] hangars. I have a page that displays the master list of ships (ShipsList) I want the user to select the ship (ShipList.name is bound to this particular ListVIew. I tried to use .Where() to filter ShipList to only the match to the selected item and copy that data to HangarCatalog. I'm getting Cannot convert GallogForms.Api.ShipCatalog to GallogForms.Api.HangarCatalog using the following code. ViewModel
private ShipCatalog _selectedShip;
public ShipCatalog SelectedShip
{
get {return _selectedShip; }
set
{if (_selectedShip != value)
_selectedShip = value;
id = _selectedShip.id;
CopyShipData();
private async void CopyShipData()
{
var _container = Items.Where(s =>
s.name.FirstOrDefault().ToString() == id.ToString()).ToList();
foreach (var item in _container.Where(s =>
s.name.FirstOrDefault().ToString() == id.ToString()).ToList())
// var items = await _gallogClient.GetItemsAsync<ShipList>();
// foreach (var item in items.ships.Where(s =>
// s.name.FirstOrDefault().ToString() == id.ToString()).ToList())
{
Hangars.Clear();
Hangars.Add(item);
}
}
I haven't found any answer yet, and I've read plenty, that can address my situation. myShipsList is bound to a new model I've created in the API that perfectly mirrors ShipCatalog[].
I've also keep running across answers that suggest ListViewItem.Item or in my case SuggestedShipView.Items. .Items is not an option for my ListViews in the view model.
AddShipPage.xaml
<StackLayout Orientation="Vertical">
<SearchBar x:Name="HangarListView" Text="Add To Your Fleet!"
TextChanged="HangarList_TextChanged"
BackgroundColor="Azure"
/>
<Grid>
<ListView x:Name="SuggestedShipView"
ItemsSource="{Binding Items}"
SelectedItem="{Binding selectedShip}"
BackgroundColor="Silver">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
.....................
ShipList.cs (API Query)
[ApiPath("ships")]
public class ShipList : ApiQueryable
{
public ShipCatalog[] ships { get; set; }
}
public class ShipCatalog : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public int id { get; set; }
public string name { get; set; }
public string uri { get; set; }
public int rsi_id { get; set; }
public string img { get; set; }
public string mfr { get; set; }
public string flyable { get; set; }
public string scu { get; set; }
public string value { get; set; }
public string bgcolor { get; set; }
public string color { get; set; }
public string role { get; set; }
public bool _isVisible { get; set; }
public bool IsVisible
{
get { return _isVisible; }
set
{
if (_isVisible != value)
{
_isVisible = value;
OnPropertyChanged();
}
}
}
}
}
HangarList mirrors ShipList perfectly with the exception it's named HangarList, public HangarCatalog[] hangars
And Finally, the query which populates ShipCatalog[] AddShipViewModel
Items.Clear();
var items = await _gallogClient.GetItemsAsync<ShipList>();
foreach (var item in items.ships.ToList())
{
Items.Add(item);
}
No error messages per se, but I have not been able to structure a method to complete this task. If you would like to see the entire project to see more of what I have going on, http://github.com/dreamsforgotten/GallogMobile