zach
zach

Reputation: 1361

Xamarin ListView not populating on XAML. Binding Issues

I am following this basic example from MSDN, but it seems like something is just slightly off. I am getting this error when I run the program

Error Message:

Binding: 'Title' property not found on 'HelloWorld.MainPage+Post', target property: 'Xamarin.Forms.TextCell.Text'

I found an article somewhere talking about setting the BindingContext, but that didn't fix anything. Any guidance would be most appreciated.

XAML code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:HelloWorld"
         x:Class="HelloWorld.MainPage">

<ListView x:Name="rssList">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Title}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

MainPage.xaml.cs Code

 using System;
  using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
using System.Xml.Linq;
using Xamarin.Forms;

namespace HelloWorld
{
    public partial class MainPage : ContentPage
    {
        ObservableCollection<Post> posts = new ObservableCollection<Post>();
        public MainPage()
        {
            InitializeComponent();

            var posts = new 
    RssFeedReader().ReadFeed(@"http://www.espn.com/espn/rss/news");
            Console.WriteLine(posts.ToList().Count);
            Console.ReadLine();

            rssList.ItemsSource = posts;

            BindingContext = this;
        }
public class RssFeedReader
        {
            public string Title { get; set; }
            public ObservableCollection<Post> ReadFeed(string url)
            {
                var rssFeed = XDocument.Load(url);

                var rssPost = from item in rssFeed.Descendants("item")
                            select new Post
                            {
                                Title = item.Element("title").Value,
                                Description = item.Element("description").Value,
                                PublishedDate = item.Element("pubDate").Value
                            };
                var myObservableCollection = new ObservableCollection<Post>(rssPost);
                return myObservableCollection;
            }
        }
  public class Post
        {
            public string PublishedDate;
            public string Description;
            public string Title;
        }

    }
}

Upvotes: 0

Views: 128

Answers (1)

EvZ
EvZ

Reputation: 12179

In order to use DataBinding with Post class, it should have properties. So at least the title should look like this:

public string Title { get; set; }

P.S.: Setting the BindingContext to self is not needed since you set the ItemSource directly without data binding engine involved.

Upvotes: 1

Related Questions