ItemGridView ItemClick on Windows Store App
本文关键字:Store App Windows on ItemClick ItemGridView | 更新日期: 2023-09-27 18:34:25
我在Windows 8商店应用程序上。在这个项目中。我需要将数据发送到另一个页面。计划是,我在 MainPage.xaml 中有一个 ItemGridView,我单击新闻的图像,我需要将该新闻的文章 ID 发送到下一页,并且必须使用该 ID 创建一篇文章。
我用这个做了这个Windows Phone 8应用程序;
private void MainLongListSelectorSelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(
new Uri(
"/DetailsPage.xaml?selectedIndex=" +
((MyWebApi.Headline) MainLongListSelector.SelectedItem).ARTICLEID, UriKind.Relative));
}
在赢商店应用程序中,我尝试这个;
private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
this.Frame.Navigate(typeof (BasicPage1),
((MyWebApi.MainPageHeadline) itemGridView.SelectedItem).ARTICLEID);
}
我也尝试过这样;
private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
var articleid = ((MyWebApi.MainPageHeadline) itemGridView.SelectedItem).ARTICLEID;
this.Frame.Navigate(typeof (BasicPage1), articleid);
}
但我明白
"对象引用未设置为对象的实例。"
在行的末尾。
我无法用这个获得 ID 的值。还有其他建议吗?
谢谢。
更新:
这是 MainPage.xaml.cs;
public sealed partial class MainPage : try012.Common.LayoutAwarePage
{
private string _json = "";
private MyWebApi.RootObject Headlines { get; set; }
public MainPage()
{
this.InitializeComponent();
}
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
const string urlt = "MyWebApiURL";
var hWebRequestt = (HttpWebRequest)WebRequest.Create(urlt);
hWebRequestt.Method = "GET";
hWebRequestt.BeginGetResponse(MainLongListSelectorLoadCompletedt, hWebRequestt);
}
private void MainLongListSelectorLoadCompletedt(IAsyncResult ar)
{
var request = (HttpWebRequest)ar.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(ar);
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
_json = streamReader.ReadToEnd();
Headlines = JsonConvert.DeserializeObject<MyWebApi.RootObject>(_json);
}
if (Headlines.MainPageHeadline.Count > 0)
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
itemGridView.ItemsSource = Headlines.MainPageHeadline.ToList();
});
}
private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
this.Frame.Navigate(typeof(BasicPage1),((MyWebApi.MainPageHeadline)e.ClickedItem).ARTICLEID);
}
}
这是BasicPage1.xaml.cs;
public sealed partial class BasicPage1 : try012.Common.LayoutAwarePage
{
public BasicPage1()
{
this.InitializeComponent();
}
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
}
protected override void SaveState(Dictionary<String, Object> pageState)
{
}
}
单击事件不需要在网格视图中选择项目,因此所选项目可能为 null。 试试这个。
private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
this.Frame.Navigate(typeof (BasicPage1),
((MyWebApi.MainPageHeadline) e.ClickedItem).ARTICLEID);
}
请注意使用e.ClickedItem
,它应该是绑定到可视元素的数据项。
试试这个,我希望它会开始工作
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
const string urlt = "MyWebApiURL";
var hWebRequestt = (HttpWebRequest)WebRequest.Create(urlt);
hWebRequestt.Method = "GET";
hWebRequestt.BeginGetResponse(MainLongListSelectorLoadCompletedt, hWebRequestt);
}
我已经添加了对 LayoutAwarePage 的 OnNavigatedTo 方法的调用