Created
December 10, 2018 21:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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