【问题标题】:Callback when image is loading in Xamarin Forms在 Xamarin Forms 中加载图像时的回调
【发布时间】:2021-02-24 20:58:29
【问题描述】:

我在 Xamarin 表单中有一个 Listview。列表中每个单元格的内容是文本和一个或多个图像。我搜索方法来检测单元格中的所有内容何时完成加载。我的想法是使用 Image 的 IsLoading 属性。图片来源是网址http。

这是加载图像列表的 FlexLayout 的代码:

 <FlexLayout  AlignContent="Center" AlignItems="Center" Direction="Row" JustifyContent="Center" Wrap="Wrap"  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" x:Name="media"   Grid.Row="1"  Margin="0,10,0,10" BindableLayout.ItemsSource="{Binding Media}">

        <BindableLayout.ItemTemplate>
                    <DataTemplate>

                              <Image x:Name="img" Source ="{Binding Url}" HeightRequest="{Binding ListHeigth}" WidthRequest="{Binding ListWidth}"  Aspect="AspectFit" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />

                    </DataTemplate>
        </BindableLayout.ItemTemplate>
 </FlexLayout>

我还没有找到使用 XAML 绑定 IsLoading 属性的方法。作为一个列表,我不知道如何在每个元素中使用这个属性:

img.PropertyChanged += (sender, args) =>
        {
            if (args.PropertyName == "IsLoading")
            {
                //do somink
            }
        };

Xaml 中的 IsLoading 属性如何绑定?或者是否有其他回调来检测单元格的所有内容都已加载?

非常感谢。

【问题讨论】:

    标签: image xaml xamarin xamarin.forms binding


    【解决方案1】:

    我还没有找到使用 XAML 绑定 IsLoading 属性的方法。

    IsLoading只有get方法,没有set方法,所以不能直接在XAML中添加绑定。

    所以我建议你使用FFImageLoading 来实现它。

    如果您想检测image 何时完成加载。可以实现Finish事件

      <ffimageloading:CachedImage 
                Finish="CachedImage_Finish"
                HorizontalOptions="Center" VerticalOptions="Center"
                WidthRequest="300" HeightRequest="300"
                DownsampleToViewSize="true"
                Source = "http://loremflickr.com/600/600/nature?filename=simple.jpg">
                </ffimageloading:CachedImage>
    

    这里是完成事件背景代码。

      private void CachedImage_Finish(object sender, FFImageLoading.Forms.CachedImageEvents.FinishEventArgs e)
            {
               
            }
    

    如果你使用MVVM,你也使用FinishCommand,然后在ViewModel中绑定一个命令。

    如果要在加载时显示加载图片,可以设置 LoadingPlaceholder = "loading.png",这里是类似的线程。

    Image Loading Event

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 2020-02-16
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多