To be able to refresh the data without restarting the app, we will add a Refresh button to the toolbar. MainViewModel is responsible for handling any logic that we want to perform, and we must expose any action as an ICommand that we can bind to.
Let's start by creating the Refresh command property on MainViewModel:
- In the Weather project, open the MainViewModel class.
- Add an ICommand property called Refresh and a get method that returns a new Command.
- Add an action as an expression to the constructor of the Command that calls the LoadData method, as shown in the following code:
public ICommand Refresh => new Command(async() =>
{
await LoadData();
});
Now that we have defined Command, we need to bind it to the user interface so that when the user clicks the toolbar button, the action will be executed.
To do this, perform the following steps:
- In the Weather app, open the MainView.xaml...