C# 在用户界面上加载Windows phone 8.1图像

C# 在用户界面上加载Windows phone 8.1图像,c#,wpf,xaml,windows-phone-8,C#,Wpf,Xaml,Windows Phone 8,我们正在开发一个应用程序,它必须在运行时附加一个图像,并将其显示到UI上。我们正在为其使用以下一组代码文件: private void Btn_Attach_File_Click(object sender, RoutedEventArgs e) { FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.FileTypeFilter.Add(".jpg");

我们正在开发一个应用程序,它必须在运行时附加一个图像,并将其显示到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;

                            }
                        }
                    }
                }
            }
        }
public异步void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
如果((args.Files!=null&&args.Files.Count>0))
{
IReadOnlyList files=args.files;
Image myImage=新图像();
foreach(文件中的存储文件)
{
if(args.ContinuationData[“Operate”]as string==“OpenImage”&&args.Files!=null&&args.Files.Count>0)
{
irandomaccesstream fileStream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
System.Diagnostics.Debug.WriteLine(文件名);
System.Diagnostics.Debug.WriteLine(file.Path);
使用(StorageItemThumbThumbThumbThumbThumbThumbThumbThumbThumbThumbThumbWait=wait file.GetThumbnailAsync(ThumbnailMode.SingleItem,190))/,ThumbnailOptions.UseCurrentScale);
{
if(thumbnail!=null&&thumbnail.Type==ThumbnailType.Image)
{
BitmapImage BitmapImage=新的BitmapImage();
bitmapImage.SetSource(缩略图);
myImage.Source=位图图像;
}
}
}
}
}
}
以上代码位于xaml.cs文件中。 但问题是,即使我正在加载文件,UI上也没有附件。 我们是否需要对相应的xaml文件进行任何更改?我们已经搜索了很多,但找不到任何解决方案


提前感谢,

首先,用xaml制作一个图像控件:

<Image Name="img"/>
当然可以创建绑定而不是使用Name属性,但这是最简单的方法

阅读文档:

还有一件事,如果您有一个文件,那么foreach循环
foreach(StorageFile-in-files)
就没有多大用处,如果您有多个文件,那么它将占用最后一个文件

请随时询问更多信息


快乐编码:)

你能告诉我们你在哪里把你的图像设置成一些UI控件吗?我想我没有把图像设置成UI控件。人迪生,你能解释一下怎么做吗?谢谢。谢谢你的回复。我必须选择多个文件,并且所有文件都应按所选顺序位于应用程序UI上。我在.xaml文件中使用ListView,并设置ListView类对象的ItemsSource属性。但这种设置只起了一次作用。
this.img.Source = myImage.Source;
//For better perf according to the docs, specify this
//this.img.Source.DecodePixelWidth = 200