locked
How to correctly load thumbnail image of WP website? RRS feed

  • Question

  • User367033 posted

    Hello, everyone. I have one trouble. In my Xamarin project i load the WP posts through WordPressPCL library. Everything cool but i cannot load thumbnail of any post. Library return collection of elements of type . Further i am convert the IEnumerable to ObservableCollection and bind to XAML.

    I have such a markup in XAML:

    `

                <ListView.ItemTemplate>
                    <DataTemplate >
                        <ViewCell>
                            <ViewCell.View>
                                <StackLayout Orientation="Vertical" BackgroundColor="Transparent"  VerticalOptions="FillAndExpand">
                                    <Label FontSize="Large" Text="----"/>
                                    <Image Source="{Binding Path=Links, Converter={x:Static local:GetFeaturedUrl.Instance}}"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"  />
                                    <Label FontSize="Large" Text="----"/>
    
                                    <Label FontSize="Large" Text="{Binding Path= Title.Rendered}"/>
    
                                </StackLayout>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
    
                </ListView.ItemTemplate>
                <ListView.Footer>
                    <StackLayout>
                        <ActivityIndicator x:Name="MoreItemIndicator" IsEnabled="{Binding LoadMoreButtonVisibility}" IsVisible="{Binding LoadMoreButtonVisibility}" IsRunning="{Binding LoadMoreButtonVisibility}" Color="Black" BackgroundColor="Transparent" VerticalOptions="Center" HorizontalOptions="Center" />
                        <Button Text="Load More" Clicked="LoadMore_OnClicked"  Command="{Binding LoadMoreItemsCommand}" x:Name="LoadMore" IsVisible="{Binding LoadMoreButtonVisibility,Converter={x:Static local:InverseBoolConverter.Instance}}"></Button>
                    </StackLayout>
                </ListView.Footer>
            </ListView>`
    

    This is my converter: ` public class GetFeaturedUrl : IValueConverter, INotifyPropertyChanged { public static GetFeaturedUrl Instance = new GetFeaturedUrl(); object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) {

            string s;
            AttachmentsDetailModel res = new AttachmentsDetailModel();
            var item = value as Links;
            WebClient web = new WebClient();
            Image image = new Image();
            return Task.Run(async () =>
              {
    
    
                  try
                  {
    
    
                      s = await web.DownloadStringTaskAsync(item.FeaturedMedia.ToList()[0].Href);
    
                      res = JsonConvert.DeserializeObject<AttachmentsDetailModel>(s);
    
                      image.Source = res.SourceURL;
    
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(e);
                      Crashes.TrackError(e);
                  }
    
    
    
    
                  OnPropertyChanged("");
    
                  return image.Source = res.SourceURL;
              }
    
             );
    
        }
    
    
        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }`
    

    Model has that scheme:

    [Serializable] public class AttachmentsDetailModel { [JsonProperty("source_url")] public string SourceURL { get; set; } }

    Please help me correct handle loading thumbnail image.

    Sunday, April 22, 2018 6:55 PM

All replies

  • User367033 posted

    I partially solved the problem. I used Extentsion and events.

    `public delegate void URLImageCollHandler();

        public event URLImageCollHandler URLImageCollReady;
    
        Collection<string> URLImageColl = new Collection<string>();
        public async Task GetImageUrlCollection(IEnumerable<Post> values)
        {
            string s;
    
            AttachmentsDetailModel res = new AttachmentsDetailModel();
            var item = values as Links;
            WebClient web = new WebClient();
            //Task.Run(Function);
    
            //async Task<Collection<string>> Function()
            //{
            try
            {
                foreach (Post value in values)
                {
                    s = await web.DownloadStringTaskAsync(value.Links.FeaturedMedia.ToList()[0].Href);
    
                    res = JsonConvert.DeserializeObject<AttachmentsDetailModel>(s);
    
    
                    URLImageColl.Add(res.SourceURL);
                }
    
                //   return URLImageColl;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Crashes.TrackError(e);
            }
    
            //return URLImageColl;
            //  }
    
    
            OnPropertyChanged("");
            URLImageCollReady?.Invoke();
            //return URLImageColl;
        }`
    

    Extentsion

    public static class EnumerableExtensions { public static IEnumerable<T> ConcatSingle<T>(this IEnumerable<T> source, T item) { return source.Concat(new[] { item }); } } and using collection `private void ItemsViewModel_URLImageCollReady() { int counter = 0;

            foreach (var item in Items)
            {
    
                try
                {
                    //_Items[counter].Links.FeaturedMedia.Add(new HttpsApiWOrgFeaturedmedia { Href = URLImageColl[counter] });
                    // Items[counter].Links.FeaturedMedia.ToList()[0].Href = URLImageColl[counter];
                    Items[counter].Links.FeaturedMedia = _Items[counter].Links.FeaturedMedia.ConcatSingle(new HttpsApiWOrgFeaturedmedia { Href = URLImageColl[counter] });
    
                    counter++;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    //  throw;
                }
    
            }
    
            OnPropertyChanged("");
        }`
    
    Friday, May 4, 2018 4:29 PM