xamarin形式:android上的listview中的Custom View搞砸了

时间:2019-02-11 19:52:04

标签: forms listview xamarin binding

我发现xamarin渲染ui非常困难。

我在listview中有一个自定义视图,其中包含一个小图标列表。每个项目可能都有一些可见的图标显示。我意识到,当我向下滚动到底部并滚动回到顶部时,没有显示应该显示该图标的那个。我将一个对象传递到自定义视图中,以告知要显示哪个对象。

列表视图中的项目,本地:ChildInfoIconsView是自定义视图。

                                        <StackLayout Orientation="Vertical" 
                                           Grid.Row="0" 
                                           Grid.Column="1"
                                            VerticalOptions="Center"
                                            HorizontalOptions="Start">                              
                                            <Label AutomationId="aChildName" Style="{StaticResource MediumBoldFont}" x:Name="childName" Text="{Binding DisplayName}" HorizontalOptions="StartAndExpand" />
                                            <local:ChildInfoIconsView 
                                                Child="{Binding .}"
                                                VerticalOptions="Fill">
                                            </local:ChildInfoIconsView> 
                                        </StackLayout>

自定义视图的主要代码

public partial class ChildInfoIconsView : ContentView
{
    public static readonly BindableProperty OrientationProperty = BindableProperty.Create(nameof(Orientation), typeof(StackOrientation), typeof(StackOrientation), StackOrientation.Vertical, BindingMode.OneWay, null);
    public static readonly BindableProperty ShowClassroomProperty = BindableProperty.Create(nameof(ShowClassroom), typeof(bool), typeof(bool), true, BindingMode.OneWay, null);
    public static readonly BindableProperty ChildProperty = BindableProperty.Create(nameof(Child), typeof(ChildModel), typeof(ChildModel), null, BindingMode.OneWay, null);

    protected override void OnPropertyChanged(string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);

        if (propertyName.Equals(nameof(Child)))
        {
            if (Child != null)
            {
                this.ImageUnauth.IsVisible = (Child.HasUnauthorized.HasValue) ? Child.HasUnauthorized.Value : false;
                this.ImageMedical.IsVisible = (Child.HasMedical.HasValue) ? Child.HasMedical.Value : false;
                this.ImageBirthday.IsVisible = Child.IsBirthday;
                this.ImageNewChild.IsVisible = !string.IsNullOrEmpty(Child.NewChildFlagImage);
                this.ImageClassroom.IsVisible = (!ShowClassroom) ? false : !string.IsNullOrEmpty(Child.Classroom);

                this.textClass.Text = Child.Classroom;
                this.textClass.IsVisible = (!ShowClassroom) ? false : !string.IsNullOrEmpty(Child.Classroom);
            } 
        }
        else if (propertyName.Equals(nameof(ShowClassroom)))
        {
            this.ImageClassroom.IsVisible = ShowClassroom;
            this.textClass.IsVisible = ShowClassroom;
        }
        else if (propertyName.Equals(nameof(Orientation)))
        {
            this.layout.Orientation = Orientation;
        }
    }

    public ChildInfoIconsView()
    {
        InitializeComponent();
    }

    public StackOrientation Orientation
    {
        get
        {
            return (StackOrientation)GetValue(OrientationProperty);
        }
        set
        {
            SetValue(OrientationProperty, value);
        }
    }

    public bool ShowClassroom
    {
        get
        {
            return (bool)GetValue(ShowClassroomProperty);
        }
        set
        {
            SetValue(ShowClassroomProperty, value);
        }
    }

    public ChildModel Child
    {
        get
        {
            return (ChildModel)GetValue(ChildProperty);
        }
        set
        {
            SetValue(ChildProperty, value);
        }
    }

使用旧版本的xamarin.forms时,它可以正常工作。更新软件包时,这会在android上发生。

0 个答案:

没有答案