Accessing the photo library and camera
The last major feature of Xamarin.Mobile is the ability to access photos in order to give users the ability to add their own content to your applications. Using a class called MediaPicker
, you can pull photos from the device's camera or photo library and optionally display your own UI for the operation.
Let's modify MessageViewModel
to support photos. First, add the following property:
public string Image { get; set; }
Next, we need to modify the following lines in the SendMessage
method:
if (string.IsNullOrEmpty(Text) && string.IsNullOrEmpty(Image)) throw new Exception("Message is blank."); //Then further down var message = await service.SendMessage(new Message { UserName = settings.User.Name, Conversation = Conversation.Id, Text = Text, Image = Image, Location = location, }); //Clear our variables Text = Image = null;
Next, we will need to modify the UI layer to prompt for photos....