我正确使用这个readonly可绑定属性getter吗?

时间:2018-05-19 00:43:07

标签: c# xamarin xamarin.forms

我正在使用的一个NuGet包具有以下bindable property可用:

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

下面的代码段是否正确实现“获取”VideoStateType?我找到了有关只读属性的主题,但没有任何与VideoStateTypes相关的内容。我在想console.WriteLine可以帮助我找到答案,但它并没有显示在我的控制台中。

是否正确实施?

private VideoStateType videoState;

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

如果你很好奇,下面是我的完整代码。基本上,我有一个工作isBusy属性,如果我愿意,可以切换false/true。但我只想在isBusy = true VideoState期间切换buffering

更新:5月20日: isBusy已更改为"private void Videoplayer_PropertyChanged"中的IsBusy

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

<?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>

1 个答案:

答案 0 :(得分:1)

如果你的XAML没有设置值,意味着BindingMode = OneWay或者在这种情况下BindMode = OneTime,那么你就可以很好地绑定它了。

但是,您IsBusy未正确解雇,因为您正在设置支持字段,并且通知永远不会发送。

VideoPlayer_PropertyChanged处理程序中将isBusy = false ...or true更改为IsBusy = false ...or true

我也不会将其称为可绑定属性......它只是一个属性,您可以绑定到任何公共或内部属性(如果是相同的程序集,则为内部属性)。在您引用的意义上使属性可绑定的方法是使用IsBusy之类的通知或使用DependencyObject使用DependencyProperty来设置属性或者如果它的收藏集以INotifyCollectionChanged的形式最常使用ObservableCollection<T>