【发布时间】:2022-01-27 18:31:39
【问题描述】:
可观察的集合没有更新,我也在使用集合视图创建类似于 Instagram 的网格布局,当我添加图像时我希望 UI 更新
if(photo.Success)
{
Photos.Add(new Photo { PhotoPath = photo.PhotoPath});
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(nameof(Photos)));
}
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<ffimageloading:CachedImage
Source="{Binding PhotoPath, Converter={StaticResource Key=path}}"
Aspect="AspectFill"
HeightRequest="130">
<ffimageloading:CachedImage.GestureRecognizers>
<TapGestureRecognizer
CommandParameter="{Binding .}"
Command="{Binding Source={x:Reference dash}, Path=BindingContext.SelectedCommand}"/>
</ffimageloading:CachedImage.GestureRecognizers>
</ffimageloading:CachedImage>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
public class PhotoPathConverter:IValueConverter
{
private string pathUrl = "https://photobucket-resized.s3.af-south-1.amazonaws.com/";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string path = string.Empty;
if(value != null)
{
path = pathUrl + (string)value;
}
return path;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
App.xaml
<converters:PhotoPathConverter x:Key="path"/>
public ObservableCollection<Photo> Photos { get; set; }
有趣的是,当我设置断点时,它会稍微延迟一下
可观察的集合没有更新,我也在使用集合视图创建类似于 Instagram 的网格布局,当我添加图像时我希望 UI 更新
【问题讨论】:
-
Converter={StaticResource Key=path}看起来不对。这并没有说明要使用哪个转换器。 -
命名有点奇怪,但这是我在 App 类样式中设置的关键“路径”。转换器确实被调用,但它没有更新
-
好的。为了让我了解正在发生的事情,请添加使关键“路径”指向该转换器的代码或 xaml。 (它可能不相关,但查看所涉及的整个代码有助于我确定我没有忽略某些东西。)
-
另外,添加
Photos的声明。 -
@ToolmakerSteve 好的,我添加了,Photos = new ObservableCollection
();在我的构造函数中。我还注意到,当我添加一个断点时,它会稍微延迟它的工作,它会更新 UI
标签: c# xamarin.forms xamarin.ios