【发布时间】:2018-09-08 05:39:42
【问题描述】:
以下是 XAML 页面中的自定义控件,用于在滑块控件中显示图像(除非要求,否则我不会包含该控件的 C# 代码)
<custom:ImageGallery ItemsSource="{Binding Images}" Grid.Row="1">
<custom:ImageGallery.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Source}" Aspect="AspectFit" >
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Path=BindingContext.PreviewImageCommand, Source={x:Reference ThePage}}"
CommandParameter="{Binding ImageId}" />
</Image.GestureRecognizers>
</Image>
</DataTemplate>
</custom:ImageGallery.ItemTemplate>
</custom:ImageGallery>
<Button
Grid.Row="2"
Text="populate"
Command="{Binding PopulateCommand}">
</Button>
当点击按钮时控件被填充。 这是按钮绑定的命令:
public ObservableCollection<GalleryImage> Images
{
get
{
return _images;
}
}
public ICommand PopulateCommand
{
get
{
return new Command(async () => await PopulateImagesCommand(), () => true);
}
}
public async Task PopulateImagesCommand()
{
// adds images to the observable collection 'Images'
}
我宁愿在页面打开后立即进行填充,而不是在单击按钮时进行填充。我试过改变
public ObservableCollection<GalleryImage> Images
{
get
{
return _images;
}
}
到
public ObservableCollection<GalleryImage> Images
{
get
{
PopulateImagesCommand();
return _images;
}
}
但这显然不起作用。 谁能在这里指出我正确的方向?
【问题讨论】:
标签: c# xaml xamarin.forms