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

カメラ.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 メソッドから画像ライブラリに保存した画像に画像ソースを設定しようとしています。

私の問題は、最初のページの画像に画像が表示されないことです! 助けてください!

4

2 に答える 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;
}

コードはこちらから。

于 2012-08-31T18:19:38.930 に答える
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;
于 2012-08-06T23:20:51.460 に答える