代码之家  ›  专栏  ›  技术社区  ›  Steztric

Xamarin。与数据触发器绑定的表单-值未更新

  •  3
  • Steztric  · 技术社区  · 7 年前

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

    这将正确显示 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

    <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}" />
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Yuri S    7 年前

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

    考虑给标签一个“默认”值的选项,并作为一个没有样式的孩子来做

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