Exercise 4: Homework: Abstracting out the shared code (XAM270)
In this homework assignment, you will pull all the sharable code out of the code-behind now that it no longer depends on the UI controls and place it into a separate class which can be unit tested.
To complete the exercise, you will need Visual Studio for Windows or macOS with the Xamarin development tools installed. You will also need either an emulator/simulator or a device to run the exercise on. Please see the setup page if you need help installing the Xamarin development environment.
Open the starter solution
This exercise is a continuation of the previous exercise. You can use your existing solution or begin from the prior Exercise 3 > Completed solution in your copy of the cloned or downloaded course materials.
Implement INotifyPropertyChanged from a view model
See if you can pull the basic flag state out of the code behind and put it into a shared class in the FlagData project. Think about how this class could use INotifyPropertyChanged
to update the BindingContext
. You will need to leave the event handlers in place for the buttons - but the actual logic can be shifted into a shared class, sometimes referred to as a "ViewModel". Check out the completed lab for a full implementation - it has a new class in the FlagData project named FunFlactsViewModel
.
Exercise summary
You can view the completed homework solution in the Exercise 4 > Completed folder of your copy of the cloned or downloaded course materials.