我有一个 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;
}`