【问题标题】:Binding multiple properties through converter to one value通过转换器将多个属性绑定到一个值
【发布时间】:2016-06-23 16:52:26
【问题描述】:

我正在使用 MvvmCross 4 并尝试绑定到 axml 中 ListView 元素的 backgroundColor。现在的问题是颜色取决于我模型中的 2 个属性:bool IsSpecialCategory 和 bool IsNsfw - 每个组合都应该得到另一种颜色。

在 iOS 中,使用 fluent 语法,在转换器中使用整个模型是没有问题的,但是在使用 axml 的 Android 中这可能吗?最简单的解决方案是在项目布局中为转换器提供整个模型,而不仅仅是一个属性:

<LinearLayout [...]
local:MvxBind="backgroundColor ItemBackgroundColorConverter(theWholeModelNotJustAProperty)">

【问题讨论】:

  • local:MvxBind="BackgroundColor CellChannelItemBackgroundColorConverter()" ...就足够了,你就得到了整个模型。有时真的很容易;)

标签: android binding xamarin converter mvvmcross


【解决方案1】:

当您可以传递额外参数时,无需将整个视图模型传递给转换器。 :)

local:MvxBind="BackgroundColor PlayerBackgroundColor(RowItem.RowId, PlayerListType)

和转换器:

public class PlayerBackgroundColorValueConverter : MvxColorValueConverter
{
    protected override MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
    {
        var playerListType = (PlayerListType)parameter;

        if (parameter != null && playerListType == PlayerListType.AllPlayers)
            return BusinessConstants.Top10BGColor;

        if ((int)value <= 10)
            return BusinessConstants.Top10BGColor;
        else
            return BusinessConstants.MyPlayersBGColor;
    }
}

【讨论】:

    【解决方案2】:

    绑定整个上下文有两种可能的语法。一个是省略属性,另一个是使用句点:

    local:MvxBind="backgroundColor ItemBackgroundColorConverter(.)"
    local:MvxBind="backgroundColor ItemBackgroundColorConverter()"
    

    请注意,这也适用于没有转换器的绑定

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2012-08-26
      • 2012-01-06
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      相关资源
      最近更新 更多