2013-10-04 34 views
0

我使用相機捕獲照片並將其保存到本地,然後單擊返回到MainPage的後退按鈕,我希望捕獲的照片顯示在我的主頁面中。我用我的主網頁下面的代碼:WIndow 8應用程序自動從本地下載圖像

MainPage.xaml.cs中:

protected async override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var parameter = e.Parameter as StorageFile; 

    if (parameter != null) 
    { 
     Uri uri = new Uri("CapturedImage.jpeg", UriKind.RelativeOrAbsolute); 
     finalImage.Source = new BitmapImage(uri); 
    } 
} 

錯誤消息是「型‘System.UriFormatException’的異常出現在System.dll中卻被沒有在用戶代碼中處理「。我該如何解決它?由於

+0

'「CapturedImage。 jpeg「'不是一個有效的絕對URI。它至多是一個相對的。 – GSerg

+0

我已經嘗試了「Relative」和「RelativeOrAbsolute」。但也不能。 –

+0

捕獲後你在哪裏保存圖像? – Xyroid

回答

1

試試這個

protected async override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var parameter = e.Parameter as StorageFile; 

    if (parameter != null) 
    { 
     using (var strm = await parameter.OpenReadAsync()) 
     { 
      var bmp = new BitmapImage(); 
      bmp.SetSource(strm); 
      finalImage.Source = bmp; 
     } 
    } 
} 

Uri僅適用於這些方案。

  • 如果文件在本地,漫遊或臨時目錄,使用Uri作爲ms-appdata:///local/CapturedImage.jpegms-appdata:///roaming/CapturedImage.jpeg

  • 如果文件是包的一部分,使用Uri作爲ms-appx:///YOUR_FOLDER/CapturedImage.jpeg

+0

感謝您的回覆。該代碼有錯誤消息「無法隱式轉換類型'無效'到'Windows.UI.Xaml.Media.ImageSource'」。如果我使用Uri作爲ms-appdata:///local/CapturedImage.jpeg或ms-appdata:///roaming/CapturedImage.jpeg,則無錯誤但無法加載圖像。 –

+0

查看更新的解決方案。 – Xyroid

相關問題