[英]Xamarin ListView not populating on XAML. Binding Issues
我正在遵循MSDN上的這個基本示例,但是似乎有些錯誤。 運行程序時出現此錯誤
錯誤信息:
Binding: 'Title' property not found on 'HelloWorld.MainPage+Post', target property: 'Xamarin.Forms.TextCell.Text'
我在某處找到有關設置BindingContext的文章,但這並沒有解決任何問題。 任何指導將不勝感激。
XAML代碼:
<?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代碼
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;
}
}
}
為了將DataBinding
與Post
類一起使用,它應該具有properties 。 因此,至少標題應如下所示:
public string Title { get; set; }
PS:不需要將BindingContext
設置為self,因為您直接設置了ItemSource
而不涉及數據綁定引擎。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.