【发布时间】:2012-12-13 23:01:25
【问题描述】:
我是 C# Windows Phone 编程新手。
简而言之,我目前正在构建一个应用程序,它将:
加载image A
加载image B
然后加载image C
然后用这3张图片做一些后期处理。
我的图像 B 和图像 C 在项目中构建为 Content。
图片 A 是从图库中选择或通过相机拍摄的,或者我们可以简单地假设图片 A 是从独立存储中加载的。
我遇到了一个我认为是异步图像加载引起的问题。
这是我的代码:
...
// I intend to load the 3 pictures by calling the method: LoadImage(int id) and LoadCImage();
else if (ListBox.SelectedIndex == 0)
{
Debug.WriteLine("Selected 0");
PhotoProcessor pp = new PhotoProcessor();
WriteableBitmap imageA = new WriteableBitmap(AImage);
WriteableBitmap imageB = LoadImage(0);
WriteableBitmap imageC = LoadCImage();
WriteableBitmap mix = pp.Mix(pp.CalcAverageColour(0), imageA, imageB, imageC);
resultPic.Source = mix;
}
...
还有:
private WriteableBitmap LoadImage(int id)
{
//String uriString = "/Assets/img0.jpg";
//BitmapImage img = new BitmapImage(new Uri(uriString, UriKind.Relative));
BitmapImage img = new BitmapImage();
img.CreateOptions = BitmapCreateOptions.None;
//img.SetSource(Application.GetResourceStream(new Uri("/Assets/facetemplate0.jpg", UriKind.Relative)).Stream);
img.UriSource = new Uri("/Assets/img" + id + ".jpg", UriKind.Relative);
//img.UriSource = new Uri(uriString, UriKind.Relative);
return new WriteableBitmap(img);
}
private WriteableBitmap LoadCImage()
{
//BitmapImage img = new BitmapImage(new Uri("/Assets/imgC.jpg", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.CreateOptions = BitmapCreateOptions.None;
//img.SetSource(Application.GetResourceStream(new Uri("/Assets/imgC.jpg", UriKind.Relative)).Stream);
bmp.UriSource = new Uri("/Assets/imgC.jpg", UriKind.Relative);
return new WriteableBitmap(bmp);
}
现在我的问题是:
当我尝试运行这段代码时,它会抛出一个空引用异常,这是因为函数mix 无法加载图像 A B 和 C(加载此图像是异步的)。
我想知道是否有办法让我按顺序加载这些图像,然后让我将它们传递给mix 函数?
我尝试过的:
-
1234563但不幸的是,我得到了同样的空引用异常。
-
我也考虑过
EventHandler方法,但我认为在这种情况下它不是一个好主意。如果我实现EventHandler,会不会是这样的(伪代码):imageA_Opened() { LoadImageB += imageB_Opened(); } imageB_Opened() { LoadImageC += imageC_Opened(); } imageC_Opened() { PhotoProcessor pp = new PhotoProcessor(); pp.Mix(averageColour, A, B, C); }
我说的对吗?
【问题讨论】:
标签: c# windows-phone-7 windows-phone