C# 未在XAML上填充Xamarin ListView。约束性问题
我遵循的是来自的这个基本示例,但似乎有点不对劲。我在运行程序时遇到此错误 错误消息:C# 未在XAML上填充Xamarin ListView。约束性问题,c#,listview,xamarin,xamarin.forms,C#,Listview,Xamarin,Xamarin.forms,我遵循的是来自的这个基本示例,但似乎有点不对劲。我在运行程序时遇到此错误 错误消息: Binding: 'Title' property not found on 'HelloWorld.MainPage+Post', target property: 'Xamarin.Forms.TextCell.Text' <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/sche
Binding: 'Title' property not found on 'HelloWorld.MainPage+Post', target property: 'Xamarin.Forms.TextCell.Text'
<?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>
我在某个地方找到一篇关于设置绑定上下文的文章,但这并没有解决任何问题。任何指导都将不胜感激
XAML代码:
Binding: 'Title' property not found on 'HelloWorld.MainPage+Post', target property: 'Xamarin.Forms.TextCell.Text'
<?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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Xml.Linq;
使用Xamarin.Forms;
名称空间HelloWorld
{
公共部分类主页:ContentPage
{
ObservableCollection posts=新的ObservableCollection();
公共主页()
{
初始化组件();
var posts=新
RssFeedReader().ReadFeed(@)http://www.espn.com/espn/rss/news");
Console.WriteLine(posts.ToList().Count);
Console.ReadLine();
rssList.ItemsSource=职位;
BindingContext=这个;
}
公共类RssFeedReader
{
公共字符串标题{get;set;}
公共ObservableCollection ReadFeed(字符串url)
{
var rssFeed=XDocument.Load(url);
var rssPost=来自rssFeed.substands中的项(“项”)
选择新职位
{
标题=项目.元素(“标题”).值,
描述=项目.元素(“描述”).值,
PublishedDate=item.Element(“pubDate”).Value
};
var myObservableCollection=新的ObservableCollection(rssPost);
返回myObservableCollection;
}
}
公营职位
{
公共字符串数据;
公共字符串描述;
公共字符串标题;
}
}
}
要将数据绑定
与Post
类一起使用,它应该具有。所以至少标题应该是这样的:
public string Title { get; set; }
注意:不需要将
BindingContext
设置为self,因为您直接设置ItemSource
,而不涉及数据绑定引擎 谢谢。我把布景放错地方了。现在工作。