I am rendering this ListView
<StackLayout>
<Label Text="Test"/> //This Label is getting rendered
<ListView ItemsSource="{Binding MusicList,Mode=TwoWay}"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
CachingStrategy="RecycleElement"
SeparatorColor="DarkGray">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Name}" TextColor="Black"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Whose ItemsSource is bound to an ObservableCollection<Music>
which is available in the ViewModel
.This is how i am binding the ViewModel
public MainPage()
{
BindingContext = new MainViewModel();
InitializeComponent();
}
This is my ViewModel
and here im calling the API methods to get the data.and
private ObservableCollection<Music> _MusicList=new ObservableCollection<Music>();
public ObservableCollection<Music> MusicList
{
get { return _MusicList; }
set
{
if (_MusicList != value)
{
_MusicList = value;
OnPropertyChanged("_MusicList");
}
}
}
public MainViewModel()
{
api = new ApiHelper();
GetSongs();
}
public async void GetSongs()
{
_MusicList = new ObservableCollection<Music>(await api.GetMusicListAsync());
}
The data is coming in the ObservableCollection<Music>
UPDATE
Just for testing I tried binding a simple string List ListStrings = new List<string> {"A","B","C","D","E","F" };
but it also didnt show up.
UPDATE Now i override onAppearing method and initialized my list and set ListView ItemsSource to it. and its working now..but dont know why its not working with binding.
MusicList = new ObservableCollection<Music>(await api.GetMusicListAsync());
with this_MusicList = new ObservableCollection<Music>(await api.GetMusicListAsync());
– MShahName
in yourMusic
class? – Dennis Schröer