我有两个ListPicker元素。其中一个ListPickers是从webclient获取数据,下一个也是基于第一个列表选择器上的selecteditem从webclient获取数据
<toolkit:ListPicker x:Name="BrandListPicker"
Background="#FAFAFA"
Foreground="#FF000000"
BorderBrush="#000000"
Margin="20,0">
</toolkit:ListPicker>
<TextBlock x:Name="BrandListPickerValue"
Visibility="Collapsed"
Text=""/>
<TextBlock Text="Models"
Foreground="#000000"
FontSize="30"
Margin="20,30,5,0" />
<toolkit:ListPicker x:Name="ModelListPicker"
Background="#FAFAFA"
Foreground="#FF000000"
BorderBrush="#000000"
Margin="20,0">
</toolkit:ListPicker>
<TextBlock x:Name="ModelListPickerValue"
Visibility="Collapsed"
Text=""/>
在页面的背面,从前一页接收一个值,然后用于生成用于在ListPicker上添加元素的链接
try {
WebClient brands_wc = new WebClient();
Uri uri = new Uri(link);
brands_wc.DownloadStringCompleted += (sender, e) =>
{
MemoryStream ms = new MemoryStream();
try
{
ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result.ToString()));
}
catch (Exception)
{
MessageBox.Show("Sorry I was unable to retrieve brands, Please try again");
//Navigate back to home
}
DataContractJsonSerializer ser = new DataContractJsonSerializer(this.BrandList.GetType());
ObservableCollection<Brands> x;
x = ser.ReadObject(ms) as ObservableCollection<Brands>;
foreach (Brands d in x)
{
int js_id = d.makeId;
String js_name = d.makeName;
BrandList.Add(new Brands()
{
makeId = js_id,
makeName = js_name,
});
BrandListPicker.Items.Add(js_name);
}
ms.Close();
};
brands_wc.DownloadStringAsync(uri);
}
catch (Exception)
{
MessageBox.Show("Please check your internet connection to load brands");
//Navigate to home page
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
String receivedCarType = "";
if (NavigationContext.QueryString.TryGetValue("CarType", out receivedCarType))
{
if (this.vehicletypes.ContainsKey(receivedCarType))
{
CarType = vehicletypes[receivedCarType];
}
}
this.GetBrands();
this.ModelListPickerValue.Text = ((ListPickerItem)ModelListPicker.SelectedItem).Content.ToString();
//this.GetModels();
//this.ModelListPickerValue.Text = ((ListPickerItem)ModelListPicker.SelectedItem).Content.ToString();
}
下面给出了一个null异常错误,为什么? - 这进一步导致无法加载下一个ListPicker
this.ModelListPickerValue.Text = ((ListPickerItem)ModelListPicker.SelectedItem).Content.ToString();