【问题标题】:Xamarin ListView not populating on XAML. Binding IssuesXamarin ListView 未填充 XAML。绑定问题
【发布时间】:2018-12-26 06:52:36
【问题描述】:

我正在关注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;
        }

    }
}

【问题讨论】:

    标签: c# listview xamarin xamarin.forms


    【解决方案1】:

    为了使用DataBindingPost 类,它应该有properties。所以至少标题应该是这样的:

    public string Title { get; set; }
    

    P.S.:不需要将 BindingContext 设置为 self,因为您直接设置了 ItemSource,而不涉及数据绑定引擎。

    【讨论】:

    • 谢谢。我把设置放在错误的地方。现在可以使用了。
    猜你喜欢
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多