简体   繁体   English

如何在Windows Phone的页面之间传递照片?

[英]How to pass a photo between pages in Windows Phone?

I know how to pass data through NavigationService.Navigate(new Uri("/Second.xaml?msg=mesage", UriKind.Relative)); 我知道如何通过NavigationService.Navigate(new Uri("/Second.xaml?msg=mesage", UriKind.Relative));传递数据NavigationService.Navigate(new Uri("/Second.xaml?msg=mesage", UriKind.Relative));

The question is, how can I pass an image selected from the library to another page? 问题是,如何将从库中选择的图像传递到另一个页面?

To select an image, I use the PhotoChooserTask and in the event where it is completed I have this: 要选择图像,我使用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;
        }
    }

How can I send the chosen photo to another page? 如何将所选照片发送到另一页? Do I have to write it in a buffer, set a global variable or 'save' it in Isolated storage? 我是否必须将其写入缓冲区,设置全局变量或将其“保存”到隔离存储中?

  1. You could save your picture in IsolatedStorage first, pass the file path to another page as a string parameter, load the picture out when you need it. 您可以先将图片保存在IsolatedStorage中,然后将文件路径作为字符串参数传递到另一页,并在需要时将图片加载出去。

  2. Use PhoneApplicationService to save the image into State, load it when you need it. 使用PhoneApplicationService将图像保存到State中,并在需要时加载它。

Sample for saving into IsolatedStorage: 保存到IsolatedStorage的样本:

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

Sample for reading picture from IsolatedStorage: 从IsolatedStorage中读取图片的示例:

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

You could use a variable defined in your app.xaml.cs and call it from your other page like so (don't mind the variable names, just a code sample I use for language support): 您可以使用在app.xaml.cs中定义的变量,然后像这样从其他页面调用它(不要介意变量名,只是我用于语言支持的代码示例):

private LanguageSingleton LanguageInstance
{
    get
    {
        return (App.Current as App).Language;
    }
}

Here is how you could define that variable: 这是定义变量的方法:

public LanguageSingleton Language { get; set; }

I'm sure there are more ways in doing this but this is one solution. 我敢肯定,还有更多的方法可以做到这一点,但这是一种解决方案。

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

相关问题 如何在Windows Phone中的页面之间传递多个数据 - How to pass multiple data between pages in windows phone 如何在Windows Phone 8.1应用程序的两个页面之间传递数据? - How to pass data between two pages in Windows Phone 8.1 application ? 我如何在页面之间传递值? (Windows Phone) - How I can pass values between pages? (Windows Phone) 在Windows Phone 8页面之间将特殊字符作为字符串传递 - Pass special characters as string between Windows Phone 8 pages 如何在Windows Phone 8.1RT中的页面之间导航 - How to navigate between pages in windows phone 8.1RT 在Windows Phone中的页面之间导航时如何保留数据 - how to retain data while navigating between pages in windows phone 如何在不同的Windows Phone页面之间共享XAML代码? - How to share xaml code between different windows phone pages? 通过/接收 <list> 在页面之间并填充listview / richtextbox C#Windows Phone 8.1 - Pass/Receive <list> between pages and fill listview/richtextbox C# Windows Phone 8.1 Windows Phone C#中的页面之间传递字符串值时,Navigation_Failed - Navigation_Failed when pass string value between pages in windows phone C# 如何在Windows Phone中实现选择照片功能 - how to implement choose photo feature in windows phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM