【问题标题】:Getting Image Source from ListBox SelectedIndex on navigation in C#在 C# 中导航时从 ListBox SelectedIndex 获取图像源
【发布时间】: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


【解决方案1】:

要满足您的要求,您还必须使 photoCollection 对 Photogallery 页面全局可用。 您可以通过从 MainPage.xaml.cs 页面将 photoCollection(您用于绑定到 Listbox)的副本分配给 App.xaml.cs 的 albumImages 来做到这一点

注意:您的代码在问题中进行了一些更改(我不确定是谁做的),但代码非常接近工作。

【讨论】:

  • 你能告诉我如何从 MainPage.xaml.cs 页面将 photoCollection 的副本分配给 App.xaml.cs 的 albumImages 吗?我很困惑。
  • //AlbumImages.cs 我已将 AlbumImages.cs 中的 ObservableCollection 更改为 public class AlbumImages: ObservableCollection&lt;photoCollection&gt; { },但我不明白。
  • 我不确定,你的 photoCollection 是什么。我假设它是 List photoCollection;在 App.xaml.cs 中,添加这个 public List albumImages;在 MainPage.xaml.cs 中,在代码的某处(当您确定 photoCollection 可用时),添加此私有 App app = App.Current as App; app.albumImages = 照片收藏;现在您也可以访问 Photogallery.xaml.cs 中的 app.albumImages
  • photoCollection 是对象的集合,即 Id、Name 和 Image,存在于名为 GalleryDataSource 的示例数据源中,该数据源使用 Expression Blend4 通过导入 xml 文件创建。
  • 如果 photoCollection 是 SampleData 源,我认为它也可以用于 phonegallery 页面。所以你可以像 phoneCollection.ElementAt(app.selectedImageIndex);如果您仍然感到困惑,请加入此chat.stackoverflow.com/rooms/9092/windows-phone
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2021-05-31
相关资源
最近更新 更多