Windows Phone - SyndicationFeed问题

5
我需要为学校项目开发Windows Phone应用程序。我按照教程做了一个RSS阅读器,但它不起作用,我不知道原因。
在运行后,我得到以下错误:
System.Windows.Data Error: BindingExpression path error: 'Items' property not found on 'Expression.Blend.SampleData.JustTestingData.JustTestingData' 'Expression.Blend.SampleData.JustTestingData.JustTestingData' (HashCode=12963143). BindingExpression: Path='Items' DataItem='Expression.Blend.SampleData.JustTestingData.JustTestingData' (HashCode=12963143); target element is 'System.Windows.Controls.ListBox' (Name='FeedContent'); target property is 'ItemsSource' (type 'System.Collections.IEnumerable').
这是我的.cs文件:
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    public void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
        wc.OpenWriteAsync(new Uri("http://www.twojapogoda.pl/wiadomosci.xml"));
    }

    public void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        SyndicationFeed feed;
        using (XmlReader reader = XmlReader.Create(e.Result))
        {
            feed = SyndicationFeed.Load(reader);
            FeedContent.ItemsSource = feed.Items;

        }
    }  
}

这是我的 XAML 代码:

<phone:PhoneApplicationPage
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Syndication="clr-namespace:System.ServiceModel.Syndication;assembly=System.ServiceModel.Syndication"
    x:Class="JustTestIt.MainPage"
    mc:Ignorable="d"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="ItemTemplate">
            <StackPanel>
                <CheckBox IsChecked="{Binding date, Mode=TwoWay}"/>
                <TextBlock Text="{Binding title}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ItemTemplate1">
            <StackPanel>
                <CheckBox IsChecked="{Binding date, Mode=TwoWay}"/>
                <TextBlock Text="{Binding title}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ItemTemplate2">
            <StackPanel Width="381">
                <TextBlock Text="{Binding title}" FontSize="32" Foreground="#FFFF8B00" Margin="0,0,10,0" FontFamily="Segoe WP Semibold"/>
                <TextBlock Text="{Binding date}" Foreground="White" FontFamily="Segoe WP Light"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="SyndicationItemTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Title.Text}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="SyndicationItemTemplate1">
            <StackPanel>
                <TextBlock Text="{Binding Title.Text}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="SyndicationItemTemplate2">
            <StackPanel>
                <TextBlock Text="{Binding Title.Text}"/>
            </StackPanel>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

    <phone:PhoneApplicationPage.FontFamily>
        <StaticResource ResourceKey="PhoneFontFamilyNormal"/>
    </phone:PhoneApplicationPage.FontFamily>
    <phone:PhoneApplicationPage.FontSize>
        <StaticResource ResourceKey="PhoneFontSizeNormal"/>
    </phone:PhoneApplicationPage.FontSize>
    <phone:PhoneApplicationPage.Foreground>
        <StaticResource ResourceKey="PhoneForegroundBrush"/>
    </phone:PhoneApplicationPage.Foreground>
    <Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Source={StaticResource JustTestingData}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <phone:Panorama Foreground="White" FontFamily="Segoe WP Light" Background="Black">
            <phone:Panorama.Title>
                <TextBlock Text="JustTest it!"/>
            </phone:Panorama.Title>
            <phone:PanoramaItem x:Name="headers" CacheMode="{x:Null}" Header="">
                <phone:PanoramaItem.RenderTransform>
                    <TranslateTransform/>
                </phone:PanoramaItem.RenderTransform>
                <Grid Margin="0">
                    <ListBox HorizontalAlignment="Left" ItemTemplate="{StaticResource ItemTemplate2}" ItemsSource="{Binding Collection}" VerticalAlignment="Top" Width="410"/>
                </Grid>
            </phone:PanoramaItem>
            <phone:PanoramaItem x:Name="articles" CacheMode="{x:Null}" Header="" d:DataContext="{d:DesignData /SampleData/SyndicationFeedSampleData.xaml}">
                <phone:PanoramaItem.RenderTransform>
                    <TranslateTransform/>
                </phone:PanoramaItem.RenderTransform>
                <Grid>
                    <ListBox x:Name="FeedContent" HorizontalAlignment="Left" ItemTemplate="{StaticResource SyndicationItemTemplate2}" ItemsSource="{Binding Items}" VerticalAlignment="Top"/>
                </Grid>
            </phone:PanoramaItem>
        </phone:Panorama>
    </Grid>
</phone:PhoneApplicationPage>

我做错了什么,导致没有从源加载任何内容? 我正在使用Blend和Visual Studio 2013。


你的样本数据是什么样子? - Rowland Shaw
这里是翻译好的文本:http://pastebin.com/xQhChGja - Djent
2个回答

5

你的错误在这一行

 <ListBox x:Name="FeedContent" HorizontalAlignment="Left" ItemTemplate="{StaticResource 
SyndicationItemTemplate2}" ItemsSource="{Binding Items}" VerticalAlignment="Top"/>

错误就在这里。
ItemsSource="{Binding Items}" 

错误提示显示找不到名为“Items”的属性。

可能有多种原因。我需要看到您的所有CS文件才能给出更具体的答案。

如果这确实是您的所有cs文件,那么存在巨大问题。您没有属性....

您可以通过添加以下内容开始解决:

public ObservableCollection Items {get;set;}

同时,看起来你没有设置数据上下文。

InitializeComponent 上方进行如下操作。

this.DataContext = this;

编辑

根据更深入的评估您的CS文件以及一些创造性的推断,看起来您需要执行以下操作。

将您的SyndicationFeed对象设置为属性。

private SyndicationFeed _feed;
public SyndicationFeed feed {get{return _feed;} set{_feed = value; OnPropertyChanged("feed");} 

将您的数据上下文设置为

this.Datacontext = feed;

实现

INotifyPropertyChanged

添加一个属性更改事件处理程序

     protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }

我很确定您的SyndicationFeed类也需要实现INotifyPropertyChanged接口。
以上两种解决方案都可以。

2
+1. OP 也可以直接从 XAML 中删除 ItemsSource,因为他在代码中设置了它。还有一个错误会阻止代码填充列表 - wc.OpenWriteAsync 应该是 wc.OpenReadAsync - 否则 wc_OpenReadCompleted 不会被触发,列表也不会被填充。 - Romasz
现在它可以工作了,当我将OpenWriteAsync改为OpenReadAsync时。但是我无法对评论结束悬赏。 - Djent

3

解释了缺少属性的错误问题(在您提供的代码中)AMR进行了说明

此外,您应该知道代码中的这行:

FeedContent.ItemsSource = feed.Items;

这会破坏你在XAML中定义的绑定。

你的FeedContent没有填充的问题与以下几行有关:

wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenWriteAsync(new Uri("http://www.twojapogoda.pl/wiadomosci.xml"));

您正在订阅事件OpenReadCompleted,但运行了OpenWriteAsync()方法。这样事件根本不会触发(而且您也可能无法写入此Uri)。请更改为:
wc.OpenReadAsync(new Uri("http://www.twojapogoda.pl/wiadomosci.xml"));

那么,当您的事件被触发并加载SyndicationFeed时。

2
@AMR 谢谢。说实话,我和自己争执了几分钟是否要发布这个答案 - 你已经回答了错误问题,但我的“提示”解决了填充问题。一开始我在等待你在我的评论后编辑你的答案(然后我会建议接受它),但你没有这样做,所以我发布了我的答案。最好的祝愿,祝你有一个愉快的一天。 - Romasz
非常感谢你们的帮助,谢谢 :) - Djent
几天前有人抢了我的东西。我不会偷你的答案。祝贺您。 - DotNetRussell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接