Xamarin形式:IValueConverter在Convert方法中接收空值 [英] Xamarin form: IValueConverter receive null value in Convert method

查看:71
本文介绍了Xamarin形式:IValueConverter在Convert方法中接收空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,其中的商品来源设置为子商品.我想将一个子对象绑定到一个视图,该视图将通过转换器设置颜色.

调用了converter方法,但是我传入的值为 null .

除点外,我还使用 Path =/,但传递给转换器的值仍然为 null .如果我绑定该属性,那很好,但不是当前项.

 < ListView x:Name ="childListView"VerticalOptions ="FillAndExpand"HasUnevenRows ="true"ItemSelected ="OnItemSelected"ItemTapped ="OnItemTapped">< ListView.ItemTemplate>< DataTemplate>< ViewCell>< ViewCell.View>< StackLayoutBackgroundColor ="{Binding.,Converter = {StaticResource accountedToColorConverter}}"Spacing ="0" Padding ="0" VerticalOptions ="FillAndExpand" Horizo​​ntalOptions ="FillAndExpand">< StackLayout Orientation ="Horizo​​ntal" Spacing ="10" Padding ="0" Horizo​​ntalOptions ="FillAndExpand" VerticalOptions ="FillAndExpand">< StackLayout Orientation ="Horizo​​ntal" Horizo​​ntalOptions ="StartAndExpand">< controls:CircleImage> 

解决方案

Phatye肯定是正确的

BackgroundColor ="{Binding.,Converter = {StaticResource accountedToColorConverter}}"

罪魁祸首.我过去也曾尝试将 {Binding.} {Binding Path =.} 与转换器一起使用,只是遇到了与您相同的null引用问题碰到.似乎Xamarin不喜欢这样.

正确的解决方案是传递要绑定到的属性的正确路径:

假设该属性是顶级属性

BackgroundColor ="{绑定路径=已记帐,Converter = {StaticResource accountedToColorConverter}}"

否则,您可以这样做:

BackgroundColor ="{Binding Path = topLevelProperty.accounted,Converter = {StaticResource accountedToColorConverter}}"

I have a list view set with item source as child. I want to bind a child object to a view which will set the color through the converter.

The converter method got called but the value i passed in was null.

Apart from dot, I also use Path=/ but the value passed to the converter still null. If i bind the property, it's fine but not the current item.

<ListView x:Name="childListView" 
    VerticalOptions="FillAndExpand" 
    HasUnevenRows="true" 
    ItemSelected="OnItemSelected"
    ItemTapped="OnItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout 
                        BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}" 
                        Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                        <StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
                                <controls:CircleImage>

解决方案

Phatye is definitely correct in saying the line

BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}"

is the culprit. I too have attempted to use the {Binding .} and {Binding Path=.} with a converter in the past only to run into the same null reference issues you're running into. It seems that Xamarin doesn't like this.

The proper solution will be to pass the proper path of the property you want to bind to:

Assuming the property is a top level property

BackgroundColor="{Binding Path=accounted, Converter={StaticResource accountedToColorConverter}}"

Otherwise you could do this:

BackgroundColor="{Binding Path=topLevelProperty.accounted, Converter={StaticResource accountedToColorConverter}}"

这篇关于Xamarin形式:IValueConverter在Convert方法中接收空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆