This Xamarin Workshop Guide was created for the The Portuguese National Meeting of IT Students (ENEI) by Sara Silva and the original content is available here. With the goal to extend it to the global community, it was published in a new project called Xam Community Workshop with the main goal for any developer or user group to be able to customize it for their events.

Before reading this article you must read:
Create the model and data source

The model

Before creating the UI, you need to define the model, for it you will define:

Each session should have a speaker (in real scenarios you can have more than one!). Figure 1 defines the class diagram from the model:


Figure 1: The model

In the ENEI.SessionsApp project create the Session and the Speaker class in a “Model” folder, as described in Figures 2 and 3:


Figure 2: Creating new Folder


Figure 3: Adding new file


The Session class can be defined by:

  1. public class Session : INotifyPropertyChanged
  2. {
  3. private int _numLikes;
  4. private Speaker _speaker;
  5. private string _date;
  6. private string _description;
  7. private string _name;
  8. private bool _isFavorite;
  9. private string _schedule;
  10. private string _room;
  11. public string Name
  12. {
  13. get { return _name; }
  14. set { _name = value; OnPropertyChanged(); }
  15. }
  16. public string Description
  17. {
  18. get { return _description; }
  19. set { _description = value; OnPropertyChanged(); }
  20. }
  21. public string Date
  22. {
  23. get { return _date; }
  24. set { _date = value; OnPropertyChanged(); }
  25. }
  26. public int NumLikes
  27. {
  28. get { return _numLikes; }
  29. set { _numLikes = value; OnPropertyChanged();}
  30. }
  31. public Speaker Speaker
  32. {
  33. get { return _speaker; }
  34. set { _speaker = value; OnPropertyChanged(); }
  35. }
  36. public bool IsFavorite
  37. {
  38. get { return _isFavorite; }
  39. set { _isFavorite = value; OnPropertyChanged();}
  40. }
  41. public string Schedule
  42. {
  43. get { return _schedule; }
  44. set { _schedule = value; OnPropertyChanged(); }
  45. }
  46. public string Room
  47. {
  48. get { return _room; }
  49. set { _room = value; OnPropertyChanged(); }
  50. }
  51. public string Details
  52. {
  53. get { return string.Format("{0} | {1} | Sala {2} ", Date, Schedule, Room); }
  54. }
  55. public event PropertyChangedEventHandler PropertyChanged;
  56. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  57. {
  58. var handler = PropertyChanged;
  59. if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  60. }
  61. }
And the Speaker class can be defined by:

  1. public class Speaker : INotifyPropertyChanged
  2. {
  3. private string _name;
  4. private string _imageUrl;
  5. public string Name
  6. {
  7. get { return _name; }
  8. set { _name = value; OnPropertyChanged();}
  9. }
  10. public string ImageUrl
  11. {
  12. get { return _imageUrl; }
  13. set { _imageUrl = value; OnPropertyChanged(); }
  14. }
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  17. {
  18. var handler = PropertyChanged;
  19. if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  20. }
  21. }
Both classes implement the interface INotifyPropertyChanged that allows notification of the UI about changes in the model and this way the data will be updated in the UI (when it is used in the bindings).

The data source

The data source will define the data that will be loaded by the application, at this moment you will have hard-coded data, but in real scenarios you should have a file, database or services to provide it.

Get the SessionsDataSource class here, or create your own data.