Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Created December 10, 2018 21:27
Show Gist options
  • Save Axemasta/7505121cd980a16419d841e5c041844d to your computer and use it in GitHub Desktop.
Save Axemasta/7505121cd980a16419d841e5c041844d to your computer and use it in GitHub Desktop.
Demonstrates how api data can be fetched from a webservice when a viewmodel is initially loaded.
public class ExampleViewModel : INotifyPropertyChanged
{
private readonly IDataObjectService _dataObjectService;
ObservableCollection<DataObject> _bindedList;
ObservableCollection<DataObject> BindedList
{
get { return _bindedList; }
}
public ExampleViewModel()
{
_dataObjectService = new DataObjectService();
Task.Run(() => GetViewModelData);
}
private Task GetViewModelData()
{
var data = await _dataObjectService.GetData();
_bindedList = new ObservableCollection(data);
OnPropertyChanged(nameof(BindedList));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment