C# Windows phone 8中的导航错误
我有一个应用程序,有几个页面;所以,我希望用户通过来回发送一些数据,在一个页面和另一个页面之间来回导航。我可以使用C# Windows phone 8中的导航错误,c#,wpf,xaml,windows-phone-8,visual-studio-2013,C#,Wpf,Xaml,Windows Phone 8,Visual Studio 2013,我有一个应用程序,有几个页面;所以,我希望用户通过来回发送一些数据,在一个页面和另一个页面之间来回导航。我可以使用 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.
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();
}
}
}
}
请问,有人能帮我解决这个错误吗?如您在评论中所述,感谢您怀疑
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));
}
你搞错了。错误是什么?你忘了提到一个黄色的强光指向
NavigationService.Navigate(新Uri(“/All Files/Product Files/Product List.xaml?pro_cate_id=“+(lstb_prod_cate.SelectedItem as ProductsDry”).DryCategoryId,UriKind.Relative))代码>,弹出窗口显示:*“System.NullReferenceException”类型的异常发生在App_Skin_Test(Final).DLL中,但未在用户代码中处理其他信息:对象引用未设置为对象的实例。在调用方法之前,检查对象是否为null。使用“new”关键字创建对象实例。获取此异常的一般帮助**如果您需要更多信息,请告诉我,谢谢错误消息告诉您一切:)(lstb_prod_cate.SelectedItem as ProductsDry)。DryCategoryId
中的某些内容为空,请调试它!也许你想看看这里的一些蓝色镜头,当没有SelectedItem并且SelectedItem类型不正确时,可能会出现NullReferenceException。我遵循了你的指示,它通过以下方式对我有效private void lstb\u prod\u cate\u SelectionChanged(对象发送方,SelectionChangedEventArgs e){if(lstb_prod_cate.SelectedItem!=null)NavigationService.Navigate(新Uri(“/All Files/Product Files/Product List.xaml?pro_cate_id=“+(lstb_prod_cate.SelectedItem as ProductsDry).DryCategoryId,UriKind.Relative));否则lstb_prod_cate.SelectedItem=1;}
非常感谢您的及时支持:)ups..请注意我最初键入的if
条件不正确。无论如何,欢迎您