Tom F
Tom F

Reputation: 21

Am I using this readonly bindable property getter correctly?

One of the NuGet packages I am using has the following bindable property available:

VideoStateType VideoState { get; } // The current state of the VideoView: Empty, Error, Buffering, Playing, Paused, Stopped

Is the code snippet below the correct implementation of "getting" the VideoStateType? I've found topics regarding Read-Only Properties, but nothing related to VideoStateTypes. I was thinking a console.WriteLine would help me find out, but it does not show up in my console.

Is it implemented properly?

private VideoStateType videoState;

public VideoStateType VideoState
{
    get 
    {
        Console.WriteLine("The VideoState is ", videoState);
        return videoState; 
    }
}

Below is my full code if you are curious. Basically, I have a working isBusy property that can be toggled false/true if I wish. But I am trying to toggle isBusy = true only during the buffering VideoState.

UPDATE: 5/20: isBusy changed to IsBusy in "private void Videoplayer_PropertyChanged"

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System;
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;
using Rox;

namespace PreAppStructure
{
    public class RoxViewModel : INotifyPropertyChanged
    {

        public RoxViewModel()
        {

        }

        // using "isBusy" binding.  
        // isBusy be manually toggled with:  bool isBusy = true/false;

        bool isBusy;

        public event PropertyChangedEventHandler PropertyChanged;

        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                OnPropertyChanged(nameof(IsBusy));
            }
        }

        // Attempting to get the "Video State" using the Read-Only Property

        private VideoStateType videoState;

        public VideoStateType VideoState
        {
            get
            {
                Console.WriteLine("The VideoState is ", videoState);
                return videoState;
            }
        }

        // Attempting to use the "Video State" in an if/else statement

        private void Videoplayer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (videoState == VideoStateType.Buffering)
            {
                IsBusy = true; // EDIT: isBusy changed to IsBusy 
            }

            else
            {
                IsBusy = false; // EDIT: isBusy changed to IsBusy
            }
        }

        //Property Changes

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs eventArgs = new PropertyChangedEventArgs(propertyName);
            PropertyChanged?.Invoke(this, eventArgs);
        }
    }
}

XAML that hosts the binding

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:PreAppStructure"
             xmlns:roxv="clr-namespace:Rox;assembly=Rox.Xamarin.Video.Portable"
             x:Class="PreAppStructure.Page3"
             Title="Welcome to page 3!">
    <Grid>
        <roxv:VideoView x:Name="VideoView" 
                        AutoPlay="True" 
                        LoopPlay="True"  
                        ShowController="True" 
                        Source="http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" 
                        PropertyChangedCommand="{Binding PropertyChangedCommand}" />

        <StackLayout BackgroundColor="Black" 
                     HorizontalOptions="Center" 
                     VerticalOptions="Center" 
                     IsVisible="{Binding IsBusy}">

            <ActivityIndicator Color="White"
                x:Name="loader"
                IsRunning="{Binding IsBusy}" 
                VerticalOptions="Center" 
                HorizontalOptions="Center"
               />

            <Label x:Name ="loadingtext" 
                   Text="Loading...Please wait!" 
                   HorizontalOptions="Center" 
                   TextColor="White" 
                   IsVisible="{Binding IsBusy}"/>

        </StackLayout>
    </Grid>
</ContentPage>

Upvotes: 1

Views: 963

Answers (1)

Michael Puckett II
Michael Puckett II

Reputation: 6749

If your XAML is not setting the value, meaning the BindingMode = OneWay or in this case BindMode = OneTime then you are binding to it just fine.

However, your IsBusy isn't firing properly because you're setting the backing field and the notification is never sent.

In the VideoPlayer_PropertyChanged handler change isBusy = false ...or true to IsBusy = false ...or true

I also wouldn't call it a Bindable Property... It's just a property and you can bind to any public or internal properties (internal if same assembly). The way to make a property bindable in a sense you're referring is to set it up with notifications like you've done with IsBusy or if using DependencyObject use DependencyProperty or if it's a collection use INotifyCollectionChanged most popularly in the form of an ObservableCollection<T>.

Upvotes: 1

Related Questions