我的應用程序中有一個主頁面和一個相機頁面。主頁面有一個沒有源代碼集和一個按鈕的圖片。如果您點擊該按鈕,它會將您帶到相機頁面。在相機頁面上,我捕獲一張圖像並將其保存在平板電腦的圖片庫中,然後導航回主要頁面,在那裏我想將圖像的來源設置爲剛拍攝並保存在圖片庫中的圖像。這是我的相關代碼。在Metro應用程序中以編程方式設置圖像源,未出現圖像
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/](http://www.c-sharpcorner.com/UploadFile/99bb20/load-image-dynamically-from-application-uri-in-windows-store /) – 2014-02-19 12:54:11