项目GridView项目单击Windows应用商店应用

时间:2013-10-08 08:34:49

标签: c# xaml windows-store-apps

我在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));
}

在win store应用程序中,我试试这个;

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)
        {
        }
    }

2 个答案:

答案 0 :(得分:3)

点击事件无需在gridview中选择项目,因此您所选项目可能为空。试试这个。

private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
    this.Frame.Navigate(typeof (BasicPage1),
        ((MyWebApi.MainPageHeadline) e.ClickedItem).ARTICLEID);
}

请注意e.ClickedItem的使用,{{1}}应该是绑定到可视元素的数据项。

答案 1 :(得分:1)

试试这个,我希望它能开始工作

 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方法