C# 如何在Windows Phone中在页面之间传递照片?

C# 如何在Windows Phone中在页面之间传递照片?,c#,visual-studio-2010,windows-phone-7,C#,Visual Studio 2010,Windows Phone 7,我知道如何传递数据 NavigationService.Navigate(新Uri(“/Second.xaml?msg=mesage”,UriKind.Relative)) 问题是,如何将从库中选择的图像传递到另一个页面 要选择图像,我使用PhotoChooserTask,如果图像已完成,我将执行以下操作: private void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.Chos

我知道如何传递数据
NavigationService.Navigate(新Uri(“/Second.xaml?msg=mesage”,UriKind.Relative))

问题是,如何将从库中选择的图像传递到另一个页面

要选择图像,我使用
PhotoChooserTask
,如果图像已完成,我将执行以下操作:

 private void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.ChosenPhoto != null)
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(e.ChosenPhoto);
            this.img.Source = image;
        }
    }

如何将所选照片发送到其他页面?我是否必须将其写入缓冲区、设置全局变量或将其“保存”在独立存储中

您可以使用app.xaml.cs中定义的变量,并从其他页面调用它,就像这样(不要介意变量名,只是我用于语言支持的代码示例):

以下是如何定义该变量:

public LanguageSingleton Language { get; set; }
我相信有更多的方法可以做到这一点,但这是一个解决方案

  • 您可以先将图片保存在IsolatedStorage中,将文件路径作为字符串参数传递到另一个页面,然后在需要时将图片加载出来

  • 使用PhoneApplicationService将图像保存到状态,并在需要时加载

  • 保存到隔离存储的示例:

    public static void SaveStreamToStorage(Stream imgStream, string fileName)
            {
                using (IsolatedStorageFile iso_storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //Structure the path you want for your file.
                    string filePath = GetImageStorePathByFileName(fileName);
    
                    //Constants.S_STORE_PATH is the path I want to store my picture.
                    if (!iso_storage.DirectoryExists(Constants.S_STORE_PATH))
                    {
                        iso_storage.CreateDirectory(Constants.S_STORE_PATH);
                    }
                    //I skip the process when I find the same file.
                    if (iso_storage.FileExists(filePath))
                    {
                        return;
                    }
    
                    try
                    {
                        if (imgStream.Length > 0)
                        {
                            using (IsolatedStorageFileStream isostream = iso_storage.CreateFile(filePath))
                            {
                                BitmapImage bitmap = new BitmapImage();
                                bitmap.SetSource(imgStream);
    
                                WriteableBitmap wb = new WriteableBitmap(bitmap);
    
                                // Encode WriteableBitmap object to a JPEG stream. 
                                Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 100);
    
                                isostream.Close();
    
                                bitmap.UriSource = null;
                                bitmap = null;
                                wb = null;
                            }
                        }
                    }
                    catch(Exception e)
                    {
                        if (iso_storage.FileExists(filePath))
                            iso_storage.DeleteFile(filePath);
    
                        throw e;
                    }
                }
            }
    
    public static BitmapImage LoadImageFromIsolatedStorage(string imgName)
            {
                try
                {
                    var bitmapImage = new BitmapImage();
                    //bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
                    using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        //Check if file exists to prevent exception when trying to access the file.
                        if (!iso.FileExists(GetImageStorePathByFileName(imgName)))
                        {
                            return null;
                        }
                        //Since I store my picture under a folder structure, I wrote a method GetImageStorePathByFileName(string fileName) to get the correct path.
                        using (var stream = iso.OpenFile(GetImageStorePathByFileName(imgName), FileMode.Open, FileAccess.Read))
                        {
                            bitmapImage.SetSource(stream);
                        }
                    }
                    //Return the picture as a bitmapImage
                    return bitmapImage;
                }
                catch (Exception e)
                {
                    // handle the exception 
                    Debug.WriteLine(e.Message);
                }
    
                return null;
            }
    
    从隔离存储中读取图片的示例:

    public static void SaveStreamToStorage(Stream imgStream, string fileName)
            {
                using (IsolatedStorageFile iso_storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //Structure the path you want for your file.
                    string filePath = GetImageStorePathByFileName(fileName);
    
                    //Constants.S_STORE_PATH is the path I want to store my picture.
                    if (!iso_storage.DirectoryExists(Constants.S_STORE_PATH))
                    {
                        iso_storage.CreateDirectory(Constants.S_STORE_PATH);
                    }
                    //I skip the process when I find the same file.
                    if (iso_storage.FileExists(filePath))
                    {
                        return;
                    }
    
                    try
                    {
                        if (imgStream.Length > 0)
                        {
                            using (IsolatedStorageFileStream isostream = iso_storage.CreateFile(filePath))
                            {
                                BitmapImage bitmap = new BitmapImage();
                                bitmap.SetSource(imgStream);
    
                                WriteableBitmap wb = new WriteableBitmap(bitmap);
    
                                // Encode WriteableBitmap object to a JPEG stream. 
                                Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 100);
    
                                isostream.Close();
    
                                bitmap.UriSource = null;
                                bitmap = null;
                                wb = null;
                            }
                        }
                    }
                    catch(Exception e)
                    {
                        if (iso_storage.FileExists(filePath))
                            iso_storage.DeleteFile(filePath);
    
                        throw e;
                    }
                }
            }
    
    public static BitmapImage LoadImageFromIsolatedStorage(string imgName)
            {
                try
                {
                    var bitmapImage = new BitmapImage();
                    //bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
                    using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        //Check if file exists to prevent exception when trying to access the file.
                        if (!iso.FileExists(GetImageStorePathByFileName(imgName)))
                        {
                            return null;
                        }
                        //Since I store my picture under a folder structure, I wrote a method GetImageStorePathByFileName(string fileName) to get the correct path.
                        using (var stream = iso.OpenFile(GetImageStorePathByFileName(imgName), FileMode.Open, FileAccess.Read))
                        {
                            bitmapImage.SetSource(stream);
                        }
                    }
                    //Return the picture as a bitmapImage
                    return bitmapImage;
                }
                catch (Exception e)
                {
                    // handle the exception 
                    Debug.WriteLine(e.Message);
                }
    
                return null;
            }