问题
If I add an item to a listview, the items is added but the image is not showed. I have to restart application to view it.
The Item is correctly added but the image is not visible.
here the 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);
}
here the 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>
Can help me? Thanks in advance
回答1:
I use your code to write a simple demo to update the images in the listView, add a item every 2 second:
Code in 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;
}
I removed the ScrollView from the ViewCell and the other xaml code is same as you.
Let have a look at the result:
Reply me if you have any questions.
回答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;
}
来源:https://stackoverflow.com/questions/55361341/xamarin-form-listview-image-binding