我有一个CarouselView
只显示一个图像,CarouselView
它绑定到一个只存储一个字符串的模型列表。
现在,当我从 ViewModel 中的构造函数向该列表添加图像时,它按预期工作,但我有一个运行的方法,OnAppearing
我希望从那里设置它。
现在,当我从该方法将图像添加到该列表时,它会将图像添加到列表中,但不会在CarouselView
.
有人知道为什么会这样吗?
我的代码:查看:
<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>
视图模型:
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 });
}
}
}