Permalink
Browse files

Implement DataBinding for play, pause and stop buttons.

- Inherit view model form INotifyPropertyChanged
- Implement NotifyPropertyChanged
- Create listened properties
- Bind the view
  • Loading branch information...
ilanolkies committed May 23, 2018
1 parent 9ab05bc commit 7c53084ed3a20fd14b1397c6a279fcc057154c84
@@ -17,8 +17,7 @@
Margin="10" />

<!-- Use Grid to keep sizing -->
<Grid x:Name="Play"
Grid.Row="1" Grid.Column="0" IsVisible="true"
<Grid Grid.Row="1" Grid.Column="0" IsVisible="{Binding DisplayPlay}"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
@@ -41,8 +40,7 @@
</Image>
</Grid>

<Grid x:Name="PauseStop"
Grid.Row="1" Grid.Column="0" IsVisible="false"
<Grid Grid.Row="1" Grid.Column="0" IsVisible="{Binding DisplayPauseStop}"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
@@ -6,7 +6,7 @@ namespace StreamingExample
{
public partial class StreamingExamplePage : ContentPage
{
private StreamingViewModel ViewModel { get { return (StreamingViewModel)this.BindingContext; } }
private StreamingViewModel ViewModel { get => (StreamingViewModel)this.BindingContext; }

public StreamingExamplePage()
{
@@ -18,23 +18,17 @@ public StreamingExamplePage()

// Callbacks to images tapped
private void Play_tapped(object sender, EventArgs e)
{
Play.IsVisible = false;
PauseStop.IsVisible = true;
{
ViewModel.Play();
}

private void Pause_tapped(object sender, EventArgs e)
{
Play.IsVisible = true;
PauseStop.IsVisible = false;
ViewModel.Pause();
}

private void Stop_tapped(object sender, EventArgs e)
{
Play.IsVisible = true;
PauseStop.IsVisible = false;
ViewModel.Stop();
}
}
@@ -1,11 +1,34 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace StreamingExample
{
public class StreamingViewModel
{
public bool IsPlaying { get; set; } = false;
public class StreamingViewModel : INotifyPropertyChanged
{
public bool DisplayPlay { get => !isPlaying; }
public bool DisplayPauseStop { get => isPlaying; }

// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

bool isPlaying;
bool IsPlaying
{
get => isPlaying;
set
{
isPlaying = value;
// Notify the property has changed
OnPropertyChanged("DisplayPlay");
OnPropertyChanged("DisplayPauseStop");
}
}

public void Play()
public void Play()
{
IsPlaying = true;
}

0 comments on commit 7c53084

Please sign in to comment.