我有一个有几个页面的应用程序;因此,我希望用户在来回发送一些数据的情况下从一个页面导航到另一个页面。我可以使用一些特定的数据导航到下一页。
NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));在下一页“ProductList.xaml”中,它可以接收发送给它的数据并进行查询。但是,在"Product“上,我放置了一个按钮,并将其事件设置为”List.xaml()“,这样用户就可以按下转到上一页了。此时,当它返回到上一页时,我在
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));
} 这是它将数据发送到下一页的第一页。
产品Category.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));
}
}
}这是下一页“产品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();
}
}
}
}拜托,有人能帮我解决这个错误吗?谢谢
发布于 2014-08-25 08:29:50
正如您在评论中指出的,您怀疑SelectedItem是null。在这种情况下,您可以通过简单的检查来避免异常,只有当SelectedItem当前不是null时才能继续您的代码:
private void lstb_prod_cate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(lstb_prod_cate.SelectedItem != null)
NavigationService.Navigate(new Uri("/All Files/Product Files/Product List.xaml?pro_cate_id="+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId, UriKind.Relative));
} https://stackoverflow.com/questions/25479793
复制相似问题