【问题标题】:xamarin form listview image bindingxamarin 表单列表视图图像绑定
【发布时间】:2019-08-17 02:00:47
【问题描述】:

如果我将项目添加到列表视图,则会添加项目但不显示图像。我必须重新启动应用程序才能查看它。

项目已正确添加,但图像不可见。

这里是cs。

ObservableCollection<Libreria> items = new ObservableCollection<Libreria>(new Libreria().GetLibrerie());

            public Home()
            {
                InitializeComponent ();



                lstLibrerie.ItemsSource = items;
                //pickerLibrerie.ItemsSource = new Libreria().GetLibrerie();


            }

            public void Reload(Libreria newLib)
            {

                items.Insert(0, newLib);

            }

这里是 xaml

 <ListView x:Name="lstLibrerie" RowHeight="120">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ScrollView> <!-- left, top, right, bottom -->
                                <StackLayout Margin="0,20,0,0" Orientation="Horizontal">
                                    <Grid HorizontalOptions="FillAndExpand">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="2*" />
                                            <ColumnDefinition Width="8*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <Image Margin="20,0,0,0" Source="{Binding Icona}" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Aspect="AspectFit" HeightRequest="120"></Image>
                                        <Label Text="{Binding Label}" Grid.Column="1" Grid.Row="0" FontAttributes="Bold"   />
                                        <Label Text="{Binding DataUltimaApertura}" Grid.Column="1" Grid.Row="1"   />
                                        <Label Text="{Binding EtichettaNrOggetti}"  Grid.Column="1" Grid.Row="2"  />
                                        <BoxView HeightRequest="1" Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" Color="Black" />
                                    </Grid>
                                </StackLayout>
                            </ScrollView>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

可以帮助我吗?提前致谢

【问题讨论】:

  • 尝试在列表视图上将 HasUnevenRows 设置为 true,并取消设置行高。我在 ListView 中发现,除非设置了这些属性,否则当发生会导致列表或单元格发生更改的更改时,它不会调整大小或重绘。
  • 感谢 max hamptom,但问题仍然存在。
  • Libreria 上的 Icona 属性是什么类型?
  • Icona 字段是一个字符串
  • 您在视单元中有滚动视图?这是一个有趣的方法。您是否尝试过删除它并让他堆叠布局?

标签: image listview xamarin.forms xamarin.forms.listview


【解决方案1】:

我用你的代码写了一个简单的demo来更新listView中的图片,每2秒添加一个项目:

主页中的代码

public partial class MainPage : ContentPage
{
    ObservableCollection<Libreria> items = new ObservableCollection<Libreria>();

    int a = 0;

    public MainPage()
    {
        InitializeComponent();

        items.Add(new Libreria("1", "Images"));

        lstLibrerie.ItemsSource = items;
        //pickerLibrerie.ItemsSource = new Libreria().GetLibrerie();          

        Device.StartTimer(TimeSpan.FromSeconds(2), () =>
        {
            if (a == 0)
            {
                Reload(new Libreria("2", "Images"));
                a = 1;
            }else if (a == 1)
            {
                Reload(new Libreria("3", "Images1"));
                a = 2;
            }
            else if(a == 2)
            {
                Reload(new Libreria("4", "Images2"));
                a = 0;
            }

            return true;
        });
    }

    public void Reload(Libreria newLib)
    {

        items.Insert(0, newLib);

    }
}

public class Libreria  : INotifyPropertyChanged
{
    string myLabel;

    public string Label
    {
        set
        {
            if (myLabel != value)
            {
                myLabel = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Label"));
                }
            }
        }
        get
        {
            return myLabel;
        }
    }

    string icon;

    public string Icona
    {
        set
        {
            if (icon != value)
            {
                icon = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Icona"));
                }
            }
        }
        get
        {
            return icon;
        }
    }

    public Libreria(string label, string cona) {

        Label = label;
        Icona = cona;        
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我从 ViewCell 中删除了 ScrollView,其他 xaml 代码与您相同。

让我们看看结果:

如果您有任何问题,请回复我。

【讨论】:

  • 非常感谢@jack hua,您的示例有效,但我的列表视图无效,我不明白我缺少什么
  • @luna80 好的,您的模型是否继承自 INotifyPropertyChanged?添加项目时标签是否更新?另外,您可以将Libreria的代码添加到您的问题中,我会为您检查。
  • 是模型继承自 INotifyPropertyChanged。标签已正确添加,但图像未正确添加。现在我将附上一个截图来更好地解释我和 Libreria 代码
  • 嗯,到目前为止我无法重现您的问题。你能在你的drawable中尝试不同的图像吗?
  • 是的,我尝试了不同的图像,但行为相同
【解决方案2】:

here the screenshot

public class Libreria : INotifyPropertyChanged
{
    [PrimaryKey]
    public Guid Id { get; set; }
    public Tipo Tipo { get; set; }
    public int IdTipo { get; set; }
    public int NrOggetti { get; set; }

    public string DataUltimaApertura { get; set; }
    string _label;
    string _icon;


    public Libreria()
    {
    }

    public string Label
    {
        set
        {
            if (_label != value)
            {
                _label = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Label"));
                }
            }
        }
        get
        {
            return _label;
        }
    }


    public string Icona
    {
        set
        {
            if (_icon != value)
            {
                _icon = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Icona"));
                }
            }
        }
        get
        {
            return _icon;
        }
    }

    public string EtichettaNrOggetti
    {
        get
        {
            return string.Format("Nr. elementi: {0}", NrOggetti);
        }
    }


public List<Libreria> GetLibrerie()
    {
        string query = string.Format("SELECT COUNT(oggetto.idlibreria) AS NrOggetti, Label, IdTipo, DataUltimaApertura, Icona FROM libreria LEFT JOIN oggetto ON libreria.id = oggetto.idlibreria GROUP BY libreria.id ORDER BY dataultimaapertura DESC");

        return App.DBConnection.Query<Libreria>(query);
        // return App.DBConnection.Table<Libreria>().OrderByDescending(lib => lib.Dataultimaapertura).ToList();
    }

    public Guid Insert()
    {
        this.Id = Guid.NewGuid();
        string query = string.Format("INSERT INTO libreria(id, idtipo, label, dataultimaapertura, icona) VALUES('" + this.Id + "'," + this.IdTipo + ",'" + this.Label + "', '" + this.DataUltimaApertura + "', '" + this.Icona + "')");

        App.DBConnection.Execute(query);

        return this.Id;

    }

    public event PropertyChangedEventHandler PropertyChanged;
}

【讨论】:

    猜你喜欢
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多