[英]Passing image between two pages
我只是通過傳遞byte[]
在兩頁之間傳遞圖像,然后嘗試使用以下代碼將byte[]
轉換為頁面2中的圖像,
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Uri.OriginalString.Contains("img"))
{
expenseDetails = AuthOrgClass.expenseDetails;
imgData = NavigationService.GetImageNavigationData();
fill();
}
}
private void fill()
{
var bitmapImage = new BitmapImage();
var memoryStream = new MemoryStream(imgData);
bitmapImage.SetSource(memoryStream);
ImageBox.Source = bitmapImage;
}
在執行行bitmapImage.SetSource(memoryStream);
我例外
System.Windows.ni.dll中發生類型'System.Exception'的異常,但未在用戶代碼中處理
可能是什么問題呢?
您應該使用IsolatedStorageSettings將圖像存儲為字節數組。 在所有應用程序中均可訪問IsolatedStorageSettings。 因此,您可以輕松地在應用程序的任何頁面之間傳遞字節數組。 試試這個可能對您有幫助。
SaveImageAsByteArray()
{
IsolatedStorageSettings MemorySettings = IsolatedStorageSettings.ApplicationSettings;
if (MemorySettings.Contains("ImageData"))
MemorySettings["ImageData"] = your byte array;
else
MemorySettings.add("ImageData", your byte array;);
IsolatedStorageSettings.ApplicationSettings.Save();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Uri.OriginalString.Contains("img"))
{
fill();
}
}
private void fill()
{
IsolatedStorageSettings MemorySettings = IsolatedStorageSettings.ApplicationSettings;
if (MemorySettings.Contains("ImageData"))
byte[] bytes = MemorySettings["ImageData"]
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.SetSource(stream);
ImageBox.Source = image;
}
最佳實踐是在App.xaml.cs中獲取一個全局Image變量,在第一頁進行分配,然后在第二頁進行獲取,這永遠不會造成問題.. :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.