将绑定的列表框数据传递到pivot页面?Windows Phone 7
本文关键字:页面 pivot Windows Phone 绑定 列表 数据 | 更新日期: 2023-09-27 18:04:51
我真头疼。这件事我急需帮助。
我有一个listbox
填充了一个公共静态void RSS提要类调用的项目。在列表框中填充了绑定项之后,单击一个项,它就会将其传递到数据透视页面。然而,当我向左或向右滑动时,我得到的都是相同的图像。这是我的问题,我想要发生的是,如果用户向左轻弹,它加载以前的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>
page page:
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);
}
}
}
一旦列表框填满,我现在就尝试将用户选择的内容传递给pivot页面。我希望相同的图像显示在枢轴中,当用户向左或向右枢轴时,它显示集合中的前一张图像或下一张图像。
Pivot Page我试图传递这个,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属性。在导航到详细信息页面后,我们必须在pivot中找到选中的项目并使其可见:
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;
};
}
设置Pivot控件的SelectedItem属性可将Pivot滚动到适当的项,并使其可见。
完整的示例可以在http://chriskoenig.net/upload/imaged.zip上看到,如果您想看到它的实际效果。
如果我没看错的话,你需要按以下方式绑定listbox:
<ListBox ItemsSource="{Binding items}" SelectedItem="{Binding SelectedFeed, Mode=TwoWay}" />
然后以同样的方式绑定Pivot:
<Pivot ItemsSource="{Binding items}" SelectedItem="{Binding SelectedFeed, Mode=TwoWay}" />
为pivot(基于Alex的代码)尝试以下操作
<Pivot ItemsSource="{Binding items}" SelectedItem="{Binding SelectedFeed, Mode=TwoWay}">
<Pivot.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Url}"/>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>
它假设在数据透视页DataContext上有相同的对象"items"提供对所有feed条目的访问,以及一个属性SelectedFeed(正如Alex提到的)支持INotifyPropertyChanged