简体   繁体   English

在界面上加载Windows Phone 8.1图像

[英]Windows phone 8.1 image loading on UI

We are developing an app which have to attach an image at the runtime and display the same to the UI. 我们正在开发一个必须在运行时附加图像并将其显示到UI的应用程序。 We are using following set of code file for it: 我们正在使用以下代码文件集:

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();
          }  

Also, to continue the app after picking file from storage, here is the code : 另外,要在从存储中选择文件后继续运行该应用,请使用以下代码:

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;

                            }
                        }
                    }
                }
            }
        }

Above code is in xaml.cs file. 上面的代码在xaml.cs文件中。 But the problem is, even though I'm loading the file, there is no attachment on UI. 但是问题是,即使我正在加载文件,UI上也没有附件。 Do we need to make any changes to the corresponding xaml file? 我们需要对相应的xaml文件进行任何更改吗? we've searched a lot on it but couldn't find any solution. 我们对此进行了很多搜索,但找不到任何解决方案。

Thanks in advance, 提前致谢,

First, make an image control in xaml: 首先,用xaml制作图像控件:

<Image Name="img"/>

In codebehind after the loop: 在循环之后的代码背后:

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

Certainly it's possible to make a binding instead of using the Name property, but this is the easiest way. 当然可以进行绑定而不使用Name属性,但这是最简单的方法。

Read the docs: MSDN 阅读文档: MSDN

One other thing, the loop foreach (StorageFile file in files) isn't that useful if you have one file, and if you have more than one file it will take the last file. 另一件事,如果有一个文件, foreach (StorageFile file in files)循环foreach (StorageFile file in files)不是那么有用,如果有多个文件,它将取最后一个文件。

Feel free to ask for more info! 随时要求更多信息!

Happy coding :)! 快乐编码:)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM