0

データを渡す方法を知っています NavigationService.Navigate(new 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;
        }
    }

選択した写真を別のページに送信するにはどうすればよいですか?バッファに書き込んだり、グローバル変数を設定したり、分離ストレージに「保存」したりする必要がありますか?

4

2 に答える 2

1
  1. 最初に画像をIsolatedStorageに保存し、ファイルパスを文字列パラメータとして別のページに渡し、必要なときに画像をロードすることができます。

  2. PhoneApplicationServiceを使用して画像をStateに保存し、必要なときにロードします。

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

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;
        }
于 2014-04-08T01:04:43.210 に答える
0

app.xaml.csで定義された変数を使用して、そのように他のページから呼び出すことができます(変数名は気にしないでください。言語サポートに使用するコードサンプルだけです)。

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

その変数を定義する方法は次のとおりです。

public LanguageSingleton Language { get; set; }

これを行うにはもっと多くの方法があると確信していますが、これは1つの解決策です。

于 2012-12-12T19:32:21.630 に答える