【发布时间】:2014-08-25 18:22:04
【问题描述】:
我有一个包含多个页面的应用程序;因此,我希望用户通过来回发送的一些数据来回导航到另一个页面。我可以使用
导航到包含一些特定数据的下一页NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
在下一页“Product List.xaml”中,它可以接收发送给它的数据并进行正常查询。但是,在“Product List.xaml”上,我放置了一个按钮并将其事件设置为“NavigationService.GoBack()”,以便用户可以按下以转到上一页。此时,回到上一页时,在
处报错 private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
}
这是它将数据发送到下一页的第一页
产品类别.xmal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml.Linq;
using App_Skin_Test_Final_.All_Files.Database_XML;
namespace App_Skin_Test_Final_.All_Files.Product_Files
{
public partial class Product_Category : PhoneApplicationPage
{
public Product_Category()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MessageBox.Show("OnNavigateTo");
base.OnNavigatedTo(e);
// Do something when the page first loaded
}
private void lstb_prod_cate_Loaded(object sender, RoutedEventArgs e)
{
XDocument xd = XDocument.Load("All Files/Database XML/ProductsDry.xml");
var data = from q in xd.Descendants("DryCategory")
orderby q.Attribute("DryCategoryName").Value
select new ProductsDry
{
DryCategoryName = q.Attribute("DryCategoryName").Value,
DryCategoryId=q.Attribute("DryCategoryId").Value
};
// MessageBox.Show();
lstb_prod_cate.DataContext = data;
}
private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
}
}
}
这是下一页“Product List.xaml.cs”,它将接收数据并可以按按钮返回
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml.Linq;
using App_Skin_Test_Final_.All_Files.Database_XML;
using System.Windows.Media.Imaging;
namespace App_Skin_Test_Final_.All_Files.Product_Files
{
public partial class Product_List : PhoneApplicationPage
{
string pro_cate_id;
public Product_List()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.TryGetValue("pro_cate_id", out pro_cate_id))
{
}
}
private void lst_product_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show(pro_cate_id);
XDocument data = XDocument.Load("All Files/Database XML/ProductsDry.xml");
var productListData = from q in data.Descendants("DryCategory")
from itemDry in q.Elements("ItemDry") // mean: itemDry in in DryCategory
where q.Attribute("DryCategoryId").Value == pro_cate_id
select new ProductsDry
{
ItemDryName = itemDry.Attribute("ItemDryName").Value,
ItemDryImage=getImage(itemDry.Attribute("ItemDryImage").Value),
ItemDryId=itemDry.Attribute("ItemDryId").Value
};
lst_product.DataContext = productListData;
// NavigationService.GoBack();
}
private System.Windows.Media.ImageSource getImage(string p)
{
return new BitmapImage(new Uri(p, UriKind.Relative));
}
private void lst_product_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/All Files/Product Files/Product Detail.xaml?itemId="+(lst_product.SelectedItem as ProductsDry).ItemDryId,UriKind.Relative));
}
private void btnGoBack_Click(object sender, RoutedEventArgs e)
{
if (this.NavigationService.CanGoBack)
{
this.NavigationService.GoBack();
}
}
}
}
请问,谁能帮我解决这个错误?谢谢
【问题讨论】:
-
您遇到了错误。错误是?你忘了提到那个
-
黄色高光指向
NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));,弹出窗口说:** App_Skin_Test(Final).DLL 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理附加信息:对象引用未设置为对象的实例。在调用方法之前检查以确定对象是否为空。使用“new”关键字创建对象实例。获取有关此异常的一般帮助** -
如果您需要更多信息,请告诉我,谢谢
-
所以错误消息告诉你一切:)
(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId里面的东西是空的,调试它!也许你想看看这里What is a NullReferenceException and how do I fix it? -
只是一些蓝色的镜头,当没有 SelectedItem 并且 SelectedItem 类型不是 PropductsDry 时,可能会出现 NullReferenceException。
标签: c# wpf xaml windows-phone-8 visual-studio-2013