Xamarin 形式的 Xaml 中的三元运算符

Mah*_*344 5 xamarin.forms

我正在开发 Xamarin 表单应用程序。我想根据属性条件设置 Grdiview 背景颜色。

我知道我可以在类文件中创建一个新属性,然后绑定到 xaml,但是有没有办法在 Xaml 本身中使用三元条件。

我的代码是

<Grid Margin="5,0,5,5" Padding="10"  BackgroundColor="White">
Run Code Online (Sandbox Code Playgroud)

该 Grid 绑定到 model ,该模型具有 IsRead 属性(可以为 null 的布尔值)。现在我想设置一个条件,当 IsRead 为 true 时,将背景颜色设置为灰色,否则设置为白色。

我怎样才能在xaml中做到这一点?

Die*_*uza 3

我从来没有听说过这样的事情,但你可以使用转换器代替。这是您最好的选择。它干净、可重复使用且易于编程。

这是针对这种需求的一个精心设计的解决方案(当然您可以通过多种方式实现,这只是其中一种)。

创建一个结构以使解决方案完全可重用:

public struct NullableBoolColorScheme
{
    public Color TrueColor { get; set; }
    public Color FalseColor { get; set; }
    public Color NullColor { get; set; }

    public NullableBoolColorScheme(Color trueColor, Color falseColor, Color nullColor)
    {
        TrueColor = trueColor;
        FalseColor = falseColor;
        NullColor = nullColor;
    }

    public Color GetColor(bool? value)
    {
        if (!value.HasValue)
            return NullColor;
        else
            return value.Value ? TrueColor : FalseColor;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建您的转换器:

public class NullableBoolToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is bool?)) return null;

        Color returningColor = Color.Default;

        if (parameter != null && parameter is NullableBoolColorScheme)
            returningColor = ((NullableBoolColorScheme)parameter).GetColor((value as bool?));

        return returningColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("Conversion not allowed");
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的 ViewModel 中处理它:

public class MyViewModel // Remember to implement INotifyPropertyChanged and raise property changes
{
    public bool? MyNullableBoolProperty
    {
        get;
        set; 
    }

    public NullableBoolColorScheme AppColorScheme { get; }

    public MyViewModel()
    {
        AppColorScheme = new NullableBoolColorScheme(Color.Gray, Color.White, Color.Transparent /* Some possibilities are open here =) */);
    }
}
Run Code Online (Sandbox Code Playgroud)

在 XAML 中使用它:

<!-- Declare your namespace -->
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:converter="clr-namespace:Namespace.Converters"
         ...
         >
<!-- Create your resources dictionary and add a converter instance -->
    <ContentPage.Resources>
        <ResourceDictionary>
            <converter:NullableBoolToColorConverter x:Key="NullableBoolToColorConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <!-- Have fun -->
    <Grid Margin="5,0,5,5" 
          Padding="10"  
          BackgroundColor="{Binding MyNullableBoolProperty, Mode=OneWay, Converter={StaticResource NullableBoolToColorConverter}, ConverteParameter={Binding AppColorScheme}}">
        ...
    </Grid>
    ...
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

我希望它对你有帮助。