UWP 图像在拖放后显示,但在稍后检索时为 NULL

UWP Image is displayed after drag and drop but is NULL when later retrieved

场景:我通过拖放到我的应用程序中获得多张图像,然后在用户交互后,这些图像(与其他数据)被拍摄并放入我最终要上传它们的对象中。

这是我的代码(我总是会删除不必要的混乱,例如:GalerieImage.Name = xy):

从拖放中接收:

if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                var Items = await e.DataView.GetStorageItemsAsync();

                foreach (StorageFile storageItem in Items)
                {
                    var DroppedImages = new BitmapImage();
                    await DroppedImages.SetSourceAsync(await storageItem.OpenReadAsync());
                    //DroppedImages.UriSource = new Uri(storageItem.Path);

                    Image GalerieImage = new Image();
                    GalerieImage.Source = DroppedImages;

                    ((RelativePanel)sender).Children.Add(GalerieImage);
                }
            }

执行此操作后,图像将在应用程序中呈现。如果我查看由此产生的每个图像,它们 (DroppedImages(BitmapImage)) 将 UriSource 设置为 NULL,即使存储项的源设置为图片的源也是如此。 (这很容易通过代码中的注释行修复。他们必须更正高度和所有内容,然后才能正确呈现。

如果我现在执行这段代码:

foreach (UIElement image in this.PanelBildgalerie.Children)
            {
                try
                {
                    if(((Image)image).Name != "no")
                    {
                        dataObject.GalerieBilder.Add((Image)image);
                    }
                }
                catch
                {

                }
            }

它在 dataObject.GalerieBilder.Add((Image)image); 处抛出一个 System.NullReferenceException,除了我给他们的名字和我设置的高度之外没有任何数据。

我的问题:我需要从这些丢弃的图像中获取图像数据,但我不知道为什么它会给我一个空引用,因为它们的渲染没有问题,而且图像的名称导致异常与正在呈现的异常匹配。

预先感谢您的帮助。

It throws a System.NullReferenceException at dataObject.GalerieBilder.Add((Image)image);

在测试过程中,我们可以使用 foreach PanelBildgalerie 的子项获取图像,并且它们的源包含值但不为空,请确保在添加图像之前初始化 GalerieBilder 集合 class。

My Problem: I need to get the image data from these images that are dropped

如果要保存丢弃的图像,可以将它们插入到 Drop 事件中的 BitmapImage 集合中,而无需从 PanelBildgalerie.Children.[=18= 中获取]

private List<BitmapImage> _images = new List<BitmapImage>();
private async void Image_Drop(object sender, DragEventArgs e)
{
    if (e.DataView.Contains(StandardDataFormats.StorageItems))
    {
        var Items = await e.DataView.GetStorageItemsAsync();

        foreach (StorageFile storageItem in Items)
        {
            var DroppedImages = new BitmapImage();
            await DroppedImages.SetSourceAsync(await storageItem.OpenReadAsync());
            //DroppedImages.UriSource = new Uri(storageItem.Path);

            Image GalerieImage = new Image();
            GalerieImage.Source = DroppedImages;

            ((RelativePanel)sender).Children.Add(GalerieImage);
            _images.Add(DroppedImages);
        }
    }
}

如果您确实要保存图片,请确保 dataObject.GalerieBilder 不为空。

private List<Image> _dropImage = new List<Image>();
private void Button_Click(object sender, RoutedEventArgs e)
{
   
    foreach (UIElement image in PanelBildgalerie.Children)
    {
        try
        {
            if (((Image)image).Name != "no")
            {
                 _dropImage.Add(image as Image);
            }
        }
        catch
        {

        }
    }
}