gpt4 book ai didi

c# - 将绑定(bind)的列表框数据传递到数据透视页? window 电话 7

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:08 25 4
gpt4 key购买 nike

弄清楚这一点让我非常头疼。我非常需要一些帮助。

我有一个 列表框,其中填充了用 public static void RSS 提要类调用的项目。一旦列表框填充了数据绑定(bind)项,我单击一个项目并将其传递到我的数据透视页。但是,当我向左或向右轻拂时,我得到的只是相同的图像。这是我的问题,我希望发生的情况是,如果用户向左滑动,它会加载以前的 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>

Page1 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; }
}
}

最佳答案

免责声明:我认为将这么多项绑定(bind)到 Pivot 控件不一定是正确的做法。你的里程可能会有所不同,但我认为更虚拟化的解决方案会更有效率。对于我的测试,它似乎表现不错,但我的小声音告诉我这里有龙......

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

在 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 属性将绑定(bind)到 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;
};
}

设置 Pivot 控件的 SelectedItem 属性可将枢轴滚动到正确的项目并使其可见。

完整示例发布在 http://chriskoenig.net/upload/imaged.zip如果你想看到它的实际效果。

关于c# - 将绑定(bind)的列表框数据传递到数据透视页? window 电话 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com