【问题标题】:Xamarin.Forms: MultiBinding IMultiValueConverter recieving Null valuesXamarin.Forms:MultiBinding IMultiValueConverter 接收 Null 值
【发布时间】:2023-03-05 04:22:01
【问题描述】:

大家好,我需要帮助。

我在绑定上使用转换器来根据对象 ID 设置背景颜色。(内容视图中的堆栈布局)

<StackLayout.BackgroundColor>
     <Binding Path="objectID" Converter="{StaticResource IntToColorConverter}"/>
</StackLayout.BackgroundColor>

这行得通。 现在我想使用多转换器(Xamarin 4.7 中的新功能)根据其他对象属性返回不同的背景色。(对于上下文:对象是一个日历条目,如果它在过去它应该是不饱和的或其他东西)

<StackLayout.BackgroundColor>
            <MultiBinding Converter="{StaticResource MultiColorConverter}">
                <Binding Path="objectID"/>
                <Binding Path="value"/>
                <Binding Path="value2"/>
            </MultiBinding>
</StackLayout.BackgroundColor>

这不起作用,因为给转换器的值都是 NULL 并且颜色变为黑色(如果所有 vslues 都是 NULL 则返回值;因此转换器也设置正确)。当我在转换器上使用断点时,它也显示了这一点,该数组仅包含 NULL 变量。

我不知道我在这里缺少什么,绑定上下文应该被继承并且不会改变。任何提示将不胜感激。

bindingcontext 在创建 ContentView 时以编程方式在内容页面上设置,我从对象列表中提供一个对象。

var evnt = new TimeTableEventView { BindingContext = Model.calenderevents[j] };

【问题讨论】:

    标签: xamarin xamarin.forms binding converters multibinding


    【解决方案1】:

    您需要返回 BindableProperty.UnsetValue 才能使用绑定 FallbackValue 。

    在xml中

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

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 2020-12-10
      • 2020-05-05
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多