在Metro应用中以编程方式设置图片来源,图片不会显示 [英] Setting Image source programatically in Metro app, Image doesn't appear

查看:56
本文介绍了在Metro应用中以编程方式设置图片来源,图片不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个主页和一个摄像头页面.主页上有一个未设置其源的图像和一个按钮.如果单击该按钮,它将带您进入相机页面.在相机页面上,我捕获图像并将其保存在平板电脑的图片库中,然后导航回主页,在该页面上,我想将图像源设置为我刚捕获并保存在图片库中的图像.这是我的相关代码.

I have a main page and a camera page in my application. The main page has an image that does not have its source set and a button. If you click the button, it will take you to the camera page. On the camera page, I capture an image and save it in the pictures library on the tablet and then navigate back to the main page where I would like to set the source of the image to the image I just captured and saved in my pictures library. Here is my relevant code.

MainPage.xaml

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

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

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方法保存到图片库的图像.

The img_OnLoaded method in the MainPage.xaml file is attempting to set the image source to the image that I save to the pictures library from the Capture_click method in the Camera.xaml.cs.

我的问题是图片永远不会出现在首页的图像上!请帮忙!

My problem is that the picture never shows up on the image on the first page! Please help!

推荐答案

这对于尝试解决从本地资源文件更新图像的相关问题的人们也可能会有所帮助.

This may also be helpful to people trying to solve the related problem of updating an Image from a local resource file.

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

代码来自此处.

这篇关于在Metro应用中以编程方式设置图片来源,图片不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆