0

我正在尝试编写一个 rssreader 并且会为一些架构提示感到高兴。我的阅读器主窗口包含两个加载到框架中的 wpf 页面,它是一个“底部栏”,用户可以在其中选择不同的 rss 提供程序。在主框架(或页面)中是我的列表视图。由于加载动画和 UI 冻结,我有一个额外的类,它带有一个后台工作程序,它用 RSS 数据填充一个可观察的集合,当我调试时,它正确地填充了我的集合。在主页中,我将 datacontext 设置为这个可观察的集合,但 listview 没有显示任何内容,我在这里卡住了。

这就是我所拥有的:

主页 XAML:

> <ListBox ItemsSource="{Binding}" DisplayMemberPath="RssTitle"
> IsSynchronizedWithCurrentItem="True"
> SelectionChanged="itemsList_SelectionChanged"
> ItemContainerStyle="{DynamicResource listboxitem_style}" Height="396"
> HorizontalAlignment="Left" Margin="126,12,0,0" Name="ListBox1"
> VerticalAlignment="Top" Width="710"></ListBox>

ListBox1.DataContext = GetRssItems.observable_list;

获取另一个 RSS 提要的底部页面:

GetRssItems getitems = new GetRssItems();
GetRssItems.observable_collection = null;
getitems.start_bg_worker("url");

GetRssItems.cs

public class GetRssItems
    {
        public static ObservableCollection<RSSItem> observable_collection { get; set; }
        public static string tmp_url;
        public BackgroundWorker worker = new BackgroundWorker();


        public void start_bg_worker(string url)
        {


            if (!worker.IsBusy)
            {

                worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                worker.RunWorkerAsync(url);
            }
        }
}

In BackgroundWorkers DoWork I'm receiving rss items with linq and add it to my observable collection:

  observable_collection.Add(new RSSItem(item.tmp_Title, item.tmp_Link, item.tmp_Description, item.tmp_pubDate, item.tmp_ImageUrl));

Seperate class RSSItem.cs

public class RSSItem
    {
         public string RssTitle { get; set; }
            public string RssLink { get; set; }
            public string RssDescription { get; set; }
            public string RsspubDate { get; set; }
            public string RssImageUrl { get; set; }

            public RSSItem(string rsstitle, string rsslink, string rssdescription, string rsspubdate, string rssimageurl)
            {
                RssTitle = rsstitle;
                RssLink = rsslink;
                RssDescription = rssdescription;
                RsspubDate = rsspubdate;
                RssImageUrl = rssimageurl;
            }
    }

Thanks for your time and hints. Best Regards

4

2 回答 2

2

You need to read up a bit MVVM to get the most benefit from WPF. Your line setting the listbox's datacontext is rather confusing.

What you should have is your main window's (xaml) data context set to a view model class that contains your observable collection. The list box's ItemsSource is set to that property name.

For example:

public class MainViewModel : INotifyPropertyChanged
{
   public ObservableCollection<RSSItem> RSSItems
   {
      get;
      set;
   }
   // Other stuff applicable to the main window.
}

When the view is constructed, pass an instance of the MainViewModel to it's DataContext. Then the Xaml for the ListBox would be:

<ListBox ItemsSource={Binding Path=RSSItems} ... />

If you want to be able to set/change the RSSItems collection instance (I.e. public setter) then you should set it up it's setter with the NotifyPropertyChanged event, however if you just add/remove items then this shouldn't be necessary. (I.e. loading populating the items in the constructor.)

于 2012-07-22T12:17:14.430 回答
1

use the following: the data context should be the Object getitems

<ListBox  ItemsSource="{Binding observable_collection}"  Height="167" Margin="0" Name="listBox1" Width="330" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Top">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding RssTitle}"  FontWeight="Bold" FontSize="16" />
                                <TextBlock Text="{Binding RssLink}"  FontSize="16"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

PS: your naming is HORRBILE

于 2012-07-22T12:08:08.090 回答