I have a CarouselView that only displays an Image, the CarouselView is Bound to a list of a Model that only stores one String.
Now when I add an image to that list from the constructor in the ViewModel it works as expected, but I have a method that runs OnAppearing and I want it to be set from there.
Now when I add an image to that list from that Method, it adds the image to the list but it does not display it in the CarouselView.
Has anybody an idea why this happens?
My code: View:
<CarouselView ItemsSource="{Binding ImageList}"
HeightRequest="260"
Loop="False"
HorizontalScrollBarVisibility="Never">
<CarouselView.ItemTemplate>
<DataTemplate x:DataType="models:CarouselImage">
<Image Source="{Binding Image}"
HeightRequest="260"
Aspect="AspectFill"/>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
ViewModel:
namespace YourPartys.ViewModels
{
[QueryProperty(nameof(Locationid), nameof(Locationid))]
public class DetailViewModel : ViewModelBase
{
#region Variables
#endregion
#region Propertys
public List<CarouselImage> ImageList { get; set; } = new List<CarouselImage>();
string locationid;
public string Locationid
{
get => locationid;
set => SetProperty(ref locationid, value);
}
LocationModel location;
public LocationModel Location
{
get => location;
set => SetProperty(ref location, value);
}
#endregion
//Constructor
public DetailViewModel()
{
Location = new LocationModel();
//ImageList.Add(new CarouselImage { Image = "https://club-l1.de/wp-content/uploads/2019/11/dsc08645-1200x800.jpg" });
}
public override async void VModelActive(Page sender, EventArgs eventArgs)
{
base.VModelActive(sender, eventArgs);
Location = await FirestoreService.GetOneLocation(Collection: "Locations", Locationid: Locationid);
//List for images
if (Location != null)
{
if (Location.Images.MainImage != null)
{ ImageList.Add(new CarouselImage() { Image = Location.Images.MainImage }); };
foreach (string _image in Location.Images.ExtraImages)
{
ImageList.Add(new CarouselImage() { Image = _image });
}
}
}
ObservableCollectioninstead ofListWorks