【发布时间】:2012-06-04 09:18:43
【问题描述】:
我有一个 ListBox 与来自 SampleData 源的图像绑定。在选择 ListBox 项时,我想在下一页中显示图像,因此我在导航时通过了 SelectedIndex,但无法获取或显示图像。我的代码如下:`
//相册图片.cs
公共类 AlbumImages: ObservableCollection {
}
//相册图片.cs
公共类 AlbumImage { 公共字符串标题 { 获取;放; }
public string content { get; set; }
}
//App.xaml.cs
private static MainViewModel viewModel = null;
public AlbumImages albumImages = new AlbumImages();
public int selectedImageIndex;
//MainPage.xaml.cs
private void listBoxPhoto_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (listBoxPhoto.SelectedIndex == -1) return;
this.NavigationService.Navigate(new Uri("/Photogallery.xaml?SelectedIndex=" + listBoxPhoto.SelectedIndex, UriKind.Relative));
}
//相册.xaml.cs
// Reference to App
private App app = App.Current as App;
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
IDictionary<string, string> parameters = this.NavigationContext.QueryString;
if (parameters.ContainsKey("SelectedIndex"))
{
app.selectedImageIndex = Int32.Parse(parameters["SelectedIndex"]);
}
else
{
app.selectedImageIndex = 0;
}
LoadImage();
}
private void LoadImage()
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource=new Uri(app.albumImages[app.selectedImageIndex].content, UriKind.RelativeOrAbsolute);
image.Source = bitmapImage;
}`
【问题讨论】:
-
不确定是否可以将 SelectedItem 作为参数传递,因为它将是您在该列表框中呈现的对象。此外,您在 SelectedItem 之前缺少 &,这可能是您无法获取该索引的原因。
-
你能再显示一些代码吗?什么是 app.selectedImageIndex?
-
@ShoaibShaikh 我已经编辑了上面的代码。实际上我想在我的应用程序中创建一个照片库。
-
this.NavigationService.Navigate(new Uri("/Photogallery.xaml?SelectedIndex=" + listBoxPhoto.SelectedIndex, UriKind.Relative));尝试将导航代码替换为上述代码。
-
@nkchandra 我已尝试使用您提供的导航代码。它获取索引,但我很困惑如何使用此索引在 Photogallery.xaml 中显示图像。
标签: c# windows-phone-7