【问题标题】:Windows phone 8.1 image loading on UI在 UI 上加载 Windows phone 8.1 图像
【发布时间】:2014-12-26 07:43:50
【问题描述】:

我们正在开发一个应用程序,该应用程序必须在运行时附加图像并将其显示到 UI。我们正在使用以下一组代码文件:

private void Btn_Attach_File_Click(object sender, RoutedEventArgs e)
{
            FileOpenPicker fileOpenPicker = new FileOpenPicker();
            fileOpenPicker.FileTypeFilter.Add(".jpg");
            fileOpenPicker.FileTypeFilter.Add(".jpeg");
            fileOpenPicker.FileTypeFilter.Add(".png");
            fileOpenPicker.ContinuationData["Operate"] = "OpenImage";
            fileOpenPicker.PickSingleFileAndContinue();
          }  

另外,要在从存储中选择文件后继续应用,这里是代码:

public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
        {
            if ((args.Files != null && args.Files.Count > 0))
            {
                IReadOnlyList<StorageFile> files = args.Files;

                Image myImage = new Image();
                foreach (StorageFile file in files)
                {
                    if (args.ContinuationData["Operate"] as string == "OpenImage" && args.Files !=          null && args.Files.Count > 0)
                    {
                        IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                        System.Diagnostics.Debug.WriteLine(file.Name);
                        System.Diagnostics.Debug.WriteLine(file.Path);

                        using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 190)) //, ThumbnailOptions.UseCurrentScale);
                        {
                            if (thumbnail != null && thumbnail.Type == ThumbnailType.Image)
                            {
                                BitmapImage bitmapImage = new BitmapImage();
                                bitmapImage.SetSource(thumbnail);

                                myImage.Source = bitmapImage;

                            }
                        }
                    }
                }
            }
        }

以上代码在 xaml.cs 文件中。 但问题是,即使我正在加载文件,UI 上也没有附件。 我们是否需要对相应的 xaml 文件进行任何更改?我们搜索了很多,但找不到任何解决方案。

提前致谢,

【问题讨论】:

  • 您能否向我们展示您将图像设置为某些 UI 控件的代码?
  • 我想我缺少 UI 控件的图像设置。 RenDishen,你能解释一下怎么做吗?谢谢。

标签: c# wpf xaml windows-phone-8


【解决方案1】:

首先,在xaml中制作一个图片控件:

<Image Name="img"/>

在循环之后的代码隐藏中:

this.img.Source = myImage.Source;
//For better perf according to the docs, specify this
//this.img.Source.DecodePixelWidth = 200

当然可以进行绑定而不是使用 Name 属性,但这是最简单的方法。

阅读文档:MSDN

另外一件事,如果您有一个文件,循环 foreach (StorageFile file in files) 就没那么有用了,如果您有多个文件,它将占用最后一个文件。

随时询问更多信息!

编码愉快:)!

【讨论】:

  • 感谢您的回复。我必须选择多个文件,并且所有文件都应按所选顺序位于应用程序 UI 上。
  • 我将 ListView 用于 .xaml 文件中的目的,并设置 ListView 类对象的 ItemsSource 属性。但设置只能工作一次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多