繁体   English   中英

将绑定的列表框数据传递到数据透视页? Windows Phone 7

[英]Passing binded listbox data to pivot page? Windows Phone 7

我对此非常头痛。 我非常需要帮助。

我有一个listbox填充有使用公共静态void RSS feed类调用的项目。 一旦列表框填充了数据绑定的项目,我单击一个项目,它将其传递到我的数据透视页。 但是,当我左右滑动时,我得到的只是同一张图像。 那是我的问题,我想发生的事情是,如果用户向左滑动,它将加载先前的RSS图像。 如果用户向右滚动,我希望它也转到下一张图片。

社区在提供指向某些内容的链接或说不使用列表框等方面有所帮助。但是,尽管我对此不陌生,但我想在代码方面提供具体帮助,以实现我的初衷。 没什么私人的-我只需要采取一些步骤,然后再处理其他我不知道的事情。

这是我所有相关的代码。

第1页Xaml:

    <ListBox x:Name="listbox" HorizontalContentAlignment="Stretch" ItemsSource="{Binding items}" SelectionChanged="listbox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                            <Image Stretch="Fill" Height="60" Width="85" Source="{Binding Url}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

后面的第1页C#代码:

 namespace Imaged
 {
  public partial class UserSubmitted : PhoneApplicationPage
  {
    private const string Myrssfeed = "http://feeds.bbci.co.uk/news/rss.xml";

    public UserSubmitted()
    {
        InitializeComponent();

        //This next function calls the RSS service, and returns the (items) and binds it to 
        //{listbox.ItemsSource = items;}. I am unable to reference the count of the items, or 
        //the array of it for some reason? The images load once the page loads.
        RssService.GetRssItems(Myrssfeed, (items) => { listbox.ItemsSource = items; }, (exception) => { MessageBox.Show(exception.Message); }, null);
    }     
   }
  }

列表框填满后,我现在尝试将用户的选择传递到数据透视页。 我希望同一图像显示在枢轴中,并且当用户向左或向右旋转时,它会显示集合中的上一个图像或下一个图像。

我正在尝试将其传递给XAML的“数据透视页面”:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Pivot Control-->
    <controls:Pivot Title="{Binding Title}">

        <!--Pivot item one-->
        <controls:PivotItem x:Name="item1">
                <Image Source="{Binding Url}"/>  <!--I take it this is causing the pics to be the same?-->
        </controls:PivotItem>

        <!--Pivot item two-->
        <controls:PivotItem x:Name="item2">
                <Image Source="{Binding Url}"/>
        </controls:PivotItem>

        <!--Pivot item three-->
        <controls:PivotItem x:Name="item3">
                <Image Source="{Binding Url}"/>
        </controls:PivotItem>

    </controls:Pivot>
</Grid>

RSS服务类被称为:

 namespace WindowsPhone.Helpers
 { 
  public class RssService
  {
    public static void GetRssItems(string rssFeed, Action<IList<RssItem>> onGetRssItemsCompleted = null, Action<Exception> onError = null, Action onFinally = null)
    {

        WebClient webClient = new WebClient();

        // register on download complete event
        webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                // convert rss result to model
                IList<RssItem> rssItems = new List<RssItem>();

                Stream stream = e.Result;
                XmlReader response = XmlReader.Create(stream);
                {
                    SyndicationFeed feeds = SyndicationFeed.Load(response);

                    foreach (SyndicationItem f in feeds.Items)
                    {
                        RssItem rssItem = new RssItem(f.Title.Text, f.Summary.Text, f.PublishDate.ToString(), f.Links[0].Uri.AbsoluteUri);
                        rssItems.Add(rssItem);
                    }
                }    

                // notify completed callback
                if (onGetRssItemsCompleted != null)
                {
                    onGetRssItemsCompleted(rssItems);
                }
            }
            finally
            {
                // notify finally callback
                if (onFinally != null)
                {
                    onFinally();
                }
            }
        };

        webClient.OpenReadAsync(new Uri(rssFeed));
     }
    }
  }

最后是RSSItem类:

namespace WindowsPhone.Helpers
{
  public class RssItem
  {
    public RssItem(string title, string summary, string publishedDate, string url)
    {
        Title = title;
        Summary = summary;
        PublishedDate = publishedDate;
        Url = url;

        // Get plain text from html
        PlainSummary = HttpUtility.HtmlDecode(Regex.Replace(summary, "<[^>]+?>", ""));
    }
    public string Title { get; set; }
    public string Summary { get; set; }
    public string PublishedDate { get; set; }
    public string Url { get; set; }
    public string PlainSummary { get; set; }
    }
  }

免责声明:我认为将这么多项目绑定到Pivot控件不一定是正确的事情。 您的里程可能会有所不同,但是我认为虚拟化解决方案会更有效。 对于我的测试,它似乎表现还可以,但是我的小声音告诉我,这里有龙……

我尽我所能重新创建了您的项目,并进行了一些增强以使其能够完成您想要的事情。 基本上,窍门是使用在主列表页面(UserSubmitted.xaml)和上面带有Pivot项的页面(PivotPage1.xaml)之间共享的ViewModel。 通过将两个页面的DataContext属性设置为相同的对象,我们能够将两个列表绑定到相同的源,从而消除了传递任何内容的需要。

在App.xaml.cs中:

public static ViewData ViewModel { get; private set; }

private void Application_Launching(object sender, LaunchingEventArgs e)
{
     // note: you should properly Tombstone this data to prevent unnecessary network access
     ViewModel = new ViewData();
}

这是ViewData的定义方式:

public class ViewData : INotifyPropertyChanged
{
    private string _FeedTitle;
    private RssItem _SelectedItem = null;
    private ObservableCollection<RssItem> _feedItems = new ObservableCollection<RssItem>();

    private const string MyRssfeed = "http://feeds.bbci.co.uk/news/rss.xml";

    public ViewData()
    {
        RssService.GetRssItems(
            MyRssfeed,
            (title, items) =>
            {
                App.Current.RootVisual.Dispatcher.BeginInvoke(() =>
                {
                    FeedTitle = title;
                    FeedItems = new ObservableCollection<RssItem>(items);
                });
            },
            (exception) =>
            {
                MessageBox.Show(exception.Message);
            },
            null);
    }

    public ObservableCollection<RssItem> FeedItems
    {
        get { return _feedItems; }
        set
        {
            if (_feedItems == value)
                return;
            _feedItems = value;
            NotifyPropertyChanged(this, new PropertyChangedEventArgs("FeedItems"));
        }
    }

    public string FeedTitle
    {
        get { return _FeedTitle; }
        set
        {
            if (_FeedTitle == value)
                return;
            _FeedTitle = value;
            NotifyPropertyChanged(this, new PropertyChangedEventArgs("FeedTitle"));
        }
    }

    public RssItem SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            if (_SelectedItem == value)
                return;
            _SelectedItem = value;
            NotifyPropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        if (PropertyChanged != null)
            PropertyChanged(sender, args);
    }
}

一旦建立,将两个页面的数据上下文属性连接到App.ViewModel相对容易。

最后一项是导航时所选项目的滚动和定位。 当您从列表页面选择一个项目时,共享ViewModel的SelectedItem属性绑定到ListBox上的SelectedItem属性。 导航到详细信息页面后,我们必须在枢轴中找到选定的项目并使之可见:

public PivotPage1()
{
    InitializeComponent();
    Loaded += (sender, e) =>
        {
            this.DataContext = App.ViewModel;
            var selectedItem = App.ViewModel.SelectedItem;
            var pi = ItemPivot.Items.First(p => p == selectedItem);
            ItemPivot.SelectedItem = pi;
        };
}

设置枢轴控件的SelectedItem属性会将枢轴滚动到适当的项目并使它可​​见。

如果您想查看完整的示例,请访问http://chriskoenig.net/upload/imaged.zip

如果我正确理解了您,则需要通过以下方式绑定列表框:

<ListBox ItemsSource="{Binding items}" SelectedItem="{Binding SelectedFeed, Mode=TwoWay}" />

然后以相同方式绑定Pivot:

<Pivot ItemsSource="{Binding items}" SelectedItem="{Binding SelectedFeed, Mode=TwoWay}" />

尝试以下操作作为枢轴(基于Alex的代码)

<Pivot ItemsSource="{Binding items}" SelectedItem="{Binding SelectedFeed, Mode=TwoWay}">
    <Pivot.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding Url}"/>
            </DataTemplate>
        </Pivot.ItemTemplate>
</Pivot>

它假定在​​数据透视页DataContext上存在相同的对象“ items”,提供对所有供稿项的访问,并具有一个SelectedSelected属性(如Alex所述),它支持INotifyPropertyChanged

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM