Xamarin.Forms 与数据触发器绑定 - 值未更新

Xamarin.Forms binding with data trigger - value not updating

我想在视频上显示经过的时间。我有一个带有格式说明符的标签,如下所示:

<Label Text="{Binding CurrentTime, StringFormat='{0:D3} seconds'}" />

这行得通,我得到了一个像 053 seconds 这样的字符串。我想在视频未播放时显示文本 Not playing,我这样指定:

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

当视频未播放时,这会正确显示 Not playing,但播放时,标签会永远卡在 000 seconds 上。出了什么问题?

编辑

视图如下所示:

<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}" />

Xaml 似乎对两种不同类型的文本绑定感到困惑 - 一种来自触发器,另一种 "direct" 它是这样工作的:

   <Label VerticalOptions="Center" HorizontalOptions="StartAndExpand">
        <Label.Triggers>
            <DataTrigger TargetType="Label" Binding="{Binding  IsPlaying}"  Value="True">
                <Setter Property="Text" Value="{Binding CurrentTime, StringFormat='{0:D3} seconds'}" />
            </DataTrigger>
            <DataTrigger TargetType="Label" Binding="{Binding  IsPlaying}"  Value="False">
                <Setter Property="Text" Value="Not playing" />
            </DataTrigger>
        </Label.Triggers>
    </Label>

考虑为标签赋予 "default" 值的选项,并在没有样式的情况下作为子项执行

    <Label VerticalOptions="Center" HorizontalOptions="StartAndExpand" Text="Not playing">
        <Label.Triggers>
            <DataTrigger TargetType="Label" Binding="{Binding  IsPlaying}"  Value="True">
                <Setter Property="Text" Value="{Binding CurrentTime, StringFormat='{0:D3} seconds'}" />
            </DataTrigger>
        </Label.Triggers>
    </Label>