Xamarin Forms:接收空值的多绑定IMultiValueConverter

Xamarin Forms:接收空值的多绑定IMultiValueConverter,xamarin,xamarin.forms,binding,converters,multibinding,Xamarin,Xamarin.forms,Binding,Converters,Multibinding,大家好,我需要帮助 我正在绑定上使用转换器根据对象id设置背景色。(contentview内部的stacklayout) 您需要返回BindableProperty.UnsetValue才能使用绑定回退值 在xaml中 暗藏 <StackLayout.BackgroundColor> <Binding Path="objectID" Converter="{StaticResource IntToColorConverter}"

大家好,我需要帮助

我正在绑定上使用转换器根据对象id设置背景色。(contentview内部的stacklayout)


您需要返回
BindableProperty.UnsetValue
才能使用绑定回退值

在xaml中 暗藏

<StackLayout.BackgroundColor>
     <Binding Path="objectID" Converter="{StaticResource IntToColorConverter}"/>
</StackLayout.BackgroundColor>
<StackLayout.BackgroundColor>
            <MultiBinding Converter="{StaticResource MultiColorConverter}">
                <Binding Path="objectID"/>
                <Binding Path="value"/>
                <Binding Path="value2"/>
            </MultiBinding>
</StackLayout.BackgroundColor>
var evnt = new TimeTableEventView { BindingContext = Model.calenderevents[j] };
<StackLayout.BackgroundColor>
            <MultiBinding Converter="{StaticResource MultiColorConverter}">
                <Binding Path="red"/>
                <Binding Path="green"/>
                <Binding Path="blue"/>
               
            </MultiBinding>
</StackLayout.BackgroundColor>
public class MultiColorConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (var value in values)
        {
            if (!(value is int b))
            {
                return Color.White;
                // set a default value when unset
            }
           
        }

        int red = (int)values[0];
        int green = (int)values[1];
        int blue = (int)values[2];

        Color color = Color.FromRgb(red,green,blue);

        return color;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
public class MyViewModel
    {
        public int red { get; set; }
        public int green { get; set; }
        public int blue { get; set; }

        public MyViewModel(int r, int g, int b)
        {
            red = r;
            green = g;
            blue = b;
        }

    }
BindingContext = new MyViewModel(120, 60, 180);