Windows Phone - SyndicationFeed问题

Dje*_*ent 5 c# xaml expression-blend windows-phone visual-studio-2013

我需要为学校项目做Windows Phone应用程序.我已经按照教程来做RSS阅读器,但它不起作用,我不知道为什么.

我收到以下错误(运行后):

System.Windows.Data错误:BindingExpression路径错误:'Expression.Blend.SampleData.JustTestingData.JustTestingData''Expression.Blend.SampleData.JustTestingData.JustTestingData'(HashCode = 12963143)上找不到'Items'属性.BindingExpression:Path ='Items'DataItem ='Expression.Blend.SampleData.JustTestingData.JustTestingData'(HashCode = 12963143); target元素是'System.Windows.Controls.ListBox'(Name ='FeedContent'); target属性是'ItemsSource'(类型'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;

        }
    }  
}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

我做错了什么,没有任何东西从源头加载?我正在使用2013年的混合和视觉工作室.

Ant*_*ell 5

您的错误就在这条线上

 <ListBox x:Name="FeedContent" HorizontalAlignment="Left" ItemTemplate="{StaticResource 
SyndicationItemTemplate2}" ItemsSource="{Binding Items}" VerticalAlignment="Top"/>
Run Code Online (Sandbox Code Playgroud)

错误就在这里

ItemsSource="{Binding Items}" 
Run Code Online (Sandbox Code Playgroud)

它说不能看到名为"物品"的财产

这可能有很多原因.我需要查看所有CS文件,以便为您提供更具体的答案

如果这是你所有的cs文件,那么就有一个巨大的问题.你没有属性....

你可以先添加它.

public ObservableCollection Items {get;set;}
Run Code Online (Sandbox Code Playgroud)

此外,您似乎没有设置datacontext.

上面InitializeComponent这样做.

this.DataContext = this;
Run Code Online (Sandbox Code Playgroud)

编辑

基于对CS文件的进一步评估和一些创造性的推断,您看起来需要执行以下操作.

使您的SyndicationFeed对象成为属性

private SyndicationFeed _feed;
public SyndicationFeed feed {get{return _feed;} set{_feed = value; OnPropertyChanged("feed");} 
Run Code Online (Sandbox Code Playgroud)

将datacontext设置为

this.Datacontext = feed;
Run Code Online (Sandbox Code Playgroud)

实行

INotifyPropertyChanged
Run Code Online (Sandbox Code Playgroud)

添加属性Changed事件处理程序

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

我很确定您的SyndicationFeed类还需要实现INotifyPropertyChanged.

上述两种解决方案中的任何一种都应该有效.

  • +1.OP也可以从xaml中删除ItemsSource,因为他在代码中设置它.还有一个错误会阻止代码填充列表 - "wc.OpenWriteAsync"应该是`wc.OpenReadAsync` - 在其他情况下,`wc_OpenReadCompleted`不会被触发并列表填充. (2认同)

Rom*_*asz 3

与丢失属性有关的错误问题(在您提供的代码中)解释了 AMR。

您还应该注意代码中的行:

FeedContent.ItemsSource = feed.Items;
Run Code Online (Sandbox Code Playgroud)

会破坏您在 xaml 中定义的 Binding。

未填充的问题FeedContent与行有关:

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

您正在订阅事件OpenReadCompleted,但正在运行OpenWriteAsync()方法。您的事件根本不会被解雇(并且您可能也无法写入此 Uri)。将其更改为:

wc.OpenReadAsync(new Uri("http://www.twojapogoda.pl/wiadomosci.xml"));
Run Code Online (Sandbox Code Playgroud)

那么您的事件将被触发并加载 SyndicateFeed。

  • @AMR 谢谢。老实说,如果要发布这个答案,我与自己斗争了几分钟 - 你已经回答了错误问题,但我的“提示”解决了填充问题。首先,我在等你在发表评论后编辑你的答案(然后我建议接受它),但你没有,所以我发布了我的答案。最美好的祝愿,祝你有美好的一天。 (2认同)