我正在尝试将一个页面的图像控件中的图像传递到另一个页面。 例如,我在 Page1.xaml 中有一个名为“image1”的图像控件,我想让“image1”中显示的图像在 Page2.xaml墨水画布中传递/显示。 你能帮我编写一下吗? 感谢。
答案 0 :(得分:1)
第一页将图像作为文件'photo.jpg'保存到隔离存储。第二页阅读它。
第1页:
public partial class Page1 : PhoneApplicationPage
{
PhotoChooserTask PhotoChooser;
int ImageWidth, ImageHeight;
// Constructor
public Page1()
{
InitializeComponent();
ImageWidth = (int)PageImage.Width;
ImageHeight = (int)PageImage.Height;
PhotoChooser = new PhotoChooserTask();
PhotoChooser.PixelWidth = ImageWidth;
PhotoChooser.PixelHeight = ImageHeight;
PhotoChooser.ShowCamera = true;
PhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooser_Completed);
}
void PhotoChooser_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("photo.jpg", FileMode.Create, FileAccess.Write, Store);
WriteableBitmap myImage = new WriteableBitmap(ImageWidth, ImageHeight);
myImage.SetSource(e.ChosenPhoto);
PageImage.Source = myImage;
PageImage.InvalidateArrange();
myImage.SaveJpeg(Stream, ImageWidth, ImageHeight, 0, 100);
Stream.Close();
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (IsolatedStorageFile.GetUserStoreForApplication().FileExists("photo.jpg"))
{
IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("photo.jpg", FileMode.Open, FileAccess.Read, Store);
WriteableBitmap myImage = new WriteableBitmap(ImageWidth, ImageHeight);
myImage.LoadJpeg(Stream);
PageImage.Source = myImage;
PageImage.InvalidateArrange();
Stream.Close();
}
}
private void ToPage2Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
}
private void LoadButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
PhotoChooser.Show();
}
}
2页:
public partial class Page2 : PhoneApplicationPage
{
int ImageWidth, ImageHeight;
public Page2()
{
InitializeComponent();
ImageWidth = (int)PageImage.Width;
ImageHeight = (int)PageImage.Height;
}
private void BackButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
NavigationService.GoBack();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (IsolatedStorageFile.GetUserStoreForApplication().FileExists("photo.jpg"))
{
IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("photo.jpg", FileMode.Open, FileAccess.Read, Store);
WriteableBitmap myImage = new WriteableBitmap(ImageWidth, ImageHeight);
myImage.LoadJpeg(Stream);
PageImage.Source = myImage;
PageImage.InvalidateArrange();
Stream.Close();
}
}
}