gpt4 book ai didi

xaml - Xamarin.Forms 与数据触发器绑定(bind) - 值不更新

转载 作者:行者123 更新时间:2023-12-01 12:19:42 25 4
gpt4 key购买 nike

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

<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 秒 上。出了什么问题?

编辑

View 看起来像这样:

<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 似乎对两种不同类型的文本绑定(bind)感到困惑 - 一种来自触发器,另一种是“直接”绑定(bind) 这是它的工作原理:

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

考虑为标签提供“默认”值的选项,并像没有样式的 child 那样做

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

关于xaml - Xamarin.Forms 与数据触发器绑定(bind) - 值不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45390836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com