在Metro应用中以编程方式设置图像源,但图像未显示出来。

4
我有一个应用程序的主页和相机页面。主页上有一张没有设置源的图片和一个按钮。如果单击该按钮,它将带您到相机页面。在相机页面上,我捕获一张图片并将其保存在平板电脑的图片库中,然后导航回主页,我想将图片的源设置为我刚刚在图片库中捕获并保存的图片。这是我的相关代码。
MainPage.xaml
<Image  x:Name="imgResume" HorizontalAlignment="Left" Height="303" Margin="975,60,0,0" Grid.Row="1" VerticalAlignment="Top" Width="360" Stretch="UniformToFill" Loaded="img_OnLoaded"/>
<Button x:Name="btnCamera" Content="Camera" HorizontalAlignment="Left" Margin="1128,372,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="2.05800008773804,0.184000000357628" Height="59" Width="108" Click="Camera_Click" IsEnabled="False"/>

MainPage.xaml.cs

private void img_OnLoaded(object sender, RoutedEventArgs e)
    {
        if (txtFirstName.Text != "" && txtLastName.Text != "")
        {
            try
            {
                imgResume.Source = ImageFromRelativePath(this, Windows.Storage.KnownFolders.PicturesLibrary.Path + ((App)Application.Current).candidate.FirstName + ((App)Application.Current).candidate.FirstName + "Resume.jpg");
                imgResume.UpdateLayout();
            }
            catch
            {
                imgResume.Source = ImageFromRelativePath(this, @"Assets/logo.png");
                imgResume.UpdateLayout();
            }
            btnCamera.IsEnabled = true;
        }
    }

    public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
    {
        var uri = new Uri(parent.BaseUri, path);
        BitmapImage result = new BitmapImage();
        result.UriSource = uri;
        return result;
    }

Camera.xaml.cs

private async void Capture_Click(object sender, RoutedEventArgs e)
    {
        if (mediaCaptureMgr != null)
        {
            string firstName = ((App)Application.Current).candidate.FirstName;
            string lastName = ((App)Application.Current).candidate.LastName;
            string fileName = firstName + lastName + "Resume.jpg";

            Windows.Storage.IStorageFile photo = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting);

            await mediaCaptureMgr.CapturePhotoToStorageFileAsync(Windows.Media.MediaProperties.ImageEncodingProperties.CreateJpeg(), photo);

            this.Frame.Navigate(typeof(BasicPersonalInfo));
        }
    }

MainPage.xaml文件中的img_OnLoaded方法试图将图片源设置为我从Camera.xaml.cs的Capture_click方法保存到图片库中的图片。

我的问题是,图片从未显示在第一页的图像上!请帮帮我!


我尝试了这两个答案,但对我都不起作用。 我在这个页面上找到了解决方案,并且完全适用于我。http://www.c-sharpcorner.com/UploadFile/99bb20/load-image-dynamically-from-application-uri-in-windows-store/ - Evan Lin
2个回答

4
这也可能对尝试解决从本地资源文件更新图像的相关问题的人有所帮助。
myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content");

public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
{
    var uri = new Uri(parent.BaseUri, path);
    BitmapImage result = new BitmapImage();
    result.UriSource = uri;
    return result;
}

这段代码来自这里


这让我走上了正确的道路。最终,我不得不取消引用图像源,然后删除图像并更新布局,以便从图像中删除图片。 - Jordan Brooker
这么多行代码来完成一个简单而常见的事情。为什么微软不能提供像 myImage = new Image(string imagePath); 这样的东西呢?可能是因为它不符合他们所订阅的任何模式或其他圈子里的规定。 - nedR

3
我认为问题出在这部分:


var uri = new Uri(parent.BaseUri, path);

看起来你试图从安装文件夹加载图像,而你的路径是完整路径,我相信在WinRT中不支持打开文件,并且即使有baseUri,也不能作为Uri使用。

你应该尝试这样做:

var file = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(((App)Application.Current).candidate.FirstName + "Resume.jpg");
var stream = await file.OpenReadAsync();
var bi = new BitmapImage();
bi.SetSource(stream);

return bi;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接