我是否正确使用了这个只读可绑定属性 getter?



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

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

下面的代码片段是否是"获取"VideoStateType的正确实现? 我找到了有关只读属性的主题,但没有与 VideoStateType 相关的主题。 我以为console.WriteLine可以帮助我找到答案,但它没有出现在我的控制台中。

它是否正确实施?

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

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

更新:5月20日:"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

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

如果 XAML 未设置值(即BindingMode = OneWay或在本例中BindMode = OneTime(,则绑定到它即可。

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

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

我也不会称它为可绑定属性...它只是一个属性,您可以绑定到任何公共或内部属性(如果程序集相同,则为内部属性(。 在您所指的意义上使属性可绑定的方法是使用通知对其进行设置,就像您使用IsBusy所做的那样,或者如果使用DependencyObject使用DependencyProperty或者如果它是集合使用INotifyCollectionChanged最流行的形式是ObservableCollection<T>

最新更新