我希望在视频中展示已经过去的时间。我有一个包含格式说明符的标签,就像这样:
代码后端看起来像这样:
<Label Text="{Binding CurrentTime, StringFormat='{0:D3} seconds'}" />
这个方法是可行的,我得到了一个类似于053秒
的字符串。当视频没有播放时,我想要显示文本未播放
,我可以像这样指定:
<Label Text="{Binding CurrentTime, StringFormat='{0:D3} seconds'}">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding IsPlaying}" Value="False">
<Setter Property="Text" Value="Not playing" />
</DataTrigger>
</Label.Triggers>
</Label>
当视频未播放时,它会正确地显示未播放
,但一旦开始播放,标签就永远停留在000秒
。出了什么问题?
编辑
视图如下所示:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyNamespace.VideoPage"
x:Name="ThePage"
BindingContext="{x:Reference Name=ThePage}">
<StackLayout>
<Label VerticalOptions="Center" Text="{Binding CurrentTime, StringFormat='{0:D3} seconds'}" HorizontalOptions="StartAndExpand">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding IsPlaying}" Value="False">
<Setter Property="Text" Value="Not playing" />
</DataTrigger>
</Label.Triggers>
</Label>
<!-- More stuff -->
</StackLayout>
</ContentPage>
代码后端看起来像这样:
public partial class VideoPage : ContentPage
{
private int currentTime;
public int CurrentTime
{
get { return currentTime; }
set
{
currentTime = value;
OnPropertyChanged();
}
}
private bool isPlaying;
public bool IsPlaying
{
get { return isPlaying; }
set
{
isPlaying = value;
OnPropertyChanged();
}
}
...
}
编辑2
在Yuri的帮助下,我用以下方法解决了这个问题。
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="Label" x:Key="PlayingStyle">
<Setter Property="Text" Value="Not playing" />
<Style.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding IsPlaying}" Value="True">
<Setter Property="Text" Value="{Binding CurrentTime, StringFormat='{0:D3} seconds'}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
...
<Label Style="{StaticResource PlayingStyle}" />