xamarin форма просмотра списка привязки изображений - PullRequest
0 голосов
/ 26 марта 2019

Если я добавляю элемент в просмотр списка, элементы добавляются, но изображение не отображается.Я должен перезапустить приложение, чтобы просмотреть его.

Элемент добавлен правильно, но изображение не видно.

здесь 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>

Может мне помочь?Заранее спасибо

Ответы [ 2 ]

0 голосов
/ 27 марта 2019

здесь скриншот

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;
}
0 голосов
/ 27 марта 2019

Я использую ваш код, чтобы написать простую демонстрацию, чтобы обновить изображения в listView, добавлять элемент каждые 2 секунды:

Код в MainPage :

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

Я удалил ScrollView из ViewCell, а другой код xaml такой же, как у вас.

Давайте посмотрим на результат:

update Image

Ответьте мне, если у вас есть какие-либо вопросы.

...