Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我的DataTrigger在我的Xamarin表单应用程序中不起作用?_Android_Ios_Xaml_Xamarin - Fatal编程技术网

Android 为什么我的DataTrigger在我的Xamarin表单应用程序中不起作用?

Android 为什么我的DataTrigger在我的Xamarin表单应用程序中不起作用?,android,ios,xaml,xamarin,Android,Ios,Xaml,Xamarin,根据Xamarins网站关于DataTriggers的文档,我想在我的ListView中实现一个解决方案,检查标签中是否有任何文本。如果Text.Length为0,则根据我的代码使用Setters设置BoxView的样式 <Label x:Name="forward_label" Text="{Binding next_charterer_info}" /> <BoxView x:Name="forward_alert" BackgroundColor="

根据Xamarins网站关于
DataTriggers
的文档,我想在我的ListView中实现一个解决方案,检查
标签中是否有任何文本。如果
Text.Length
为0,则根据我的代码使用
Setters
设置
BoxView
的样式

<Label x:Name="forward_label" Text="{Binding next_charterer_info}" />

<BoxView x:Name="forward_alert" 
         BackgroundColor="Red">
    <BoxView.Triggers>
        <DataTrigger TargetType="BoxView" 
                     Binding="{Binding Source={x:Reference forward_label}, 
                               Path=Text.Length}" Value="0">
            <Setter Property="BackgroundColor" Value="White" />
        </DataTrigger>
    </BoxView.Triggers>
</BoxView>

如您所见,我有一个标签,它被检查为
text.length
,如果它为0,则
BoxView
BackgroundColor
设置为白色。但是,当我运行应用程序时,它不工作。这一条件似乎从未得到满足。绑定数据要么有值,要么返回空字符串


有谁能帮助我解决这种方法的错误。

您是否尝试过使用值转换器,我认为这可能是一个更简单的解决方案

using Xamarin.Forms;

namespace YourProject.Converters
{
    public class StringLengthToColorConverter : IValueConverter
    {
        public Color EmptyColor { get; set; }
        public Color DefaultColor { get; set; }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || String.IsNullOrEmpty(value.ToString()))
            {
                return EmptyColor;
            }
            return DefaultColor;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
你的页面应该看起来像

<Page.Resources>
    <ResourceDictionary>
       <conv:StringLengthToColorConverter x:Key="StringLengthToColorConverter" EmptyColor="White" DefaultColor="Red" />
    </ResourceDictionary>
</Page.Resources>

<Label x:Name="forward_label" Text="{Binding next_charterer_info}" />

<BoxView x:Name="forward_alert" 
         BackgroundColor="{Binding next_charterer_info,Converter={StaticResource StringLengthToColorConverter}}">
</BoxView>