0

我使用相机拍摄照片并将其保存到本地,然后单击返回按钮返回 MainPage,我希望将拍摄的照片显示在我的主页中。我在主页中使用以下代码:

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.dll 中发生'System.UriFormatException' 类型的异常,但未在用户代码中处理”。如何解决?谢谢

4

1 回答 1

1

Try this

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 works only for these scenarios.

  • If file is in local, roaming or temp directory, use Uri as ms-appdata:///local/CapturedImage.jpeg or ms-appdata:///roaming/CapturedImage.jpeg

  • If file is part of package, use Uri as ms-appx:///YOUR_FOLDER/CapturedImage.jpeg

于 2013-10-04T11:05:10.320 回答