如果我有这样的界面:
public interface IImageService
{
ObservableCollection<PictureModel> RefreshSavedImages();
void PhotoChooserWithCameraServiceShow();
}
他的实施是:
public class ImageService: IImageService
{
public ObservableCollection<PictureModel> RefreshSavedImages()
{
// Refreash images from library
}
public WriteableBitmap StreamToWriteableBitmap(Stream imageStream)
{
WriteableBitmap image = new WriteableBitmap(1024, 768);
image.SetSource(imageStream);
imageStream.Dispose();
return image;
}
public void PhotoChooserWithCameraServiceShow()
{
// PhotoChooserTask get source of image.
}
}
当我在ViewModel中实现时:
private readonly IImageService imageService;
public MainViewModel(IImageService imageService)
{
this.imageService = imageService;
}
private void MethodA()
{
// Choose Photo, I edited it and after saved it.
imageService.PhotoChooserWithCameraServiceShow();
// Refreash the library and Bind it in my ItemSource from my ListBox.
PictureList = imageService.RefreshSavedImages();
}
我的问题是我需要先完成这个方法:imageService.PhotoChooserWithCameraServiceShow();继续使用imageService.RefreshSavedImages();
问题在于我的程序所做的是在第一次完成之前执行第二次。
我认为可能导致此问题的问题:
从ViewModel调用无需逻辑的NavigationService到goback。所以不能:
NavigationService navigation = new NavigationService(); navigation.NavigateTo(new Uri(“/ Views / SecondPage.xaml”,UriKind.Relative));
PhotoChooserWithCameraServiceShow取自Cimbalino Windows Phone Toolkit
感谢所有人和问候!
答案 0 :(得分:2)
您需要为照片选择器任务的已完成事件设置事件侦听器,并从那里调用imageService.RefreshSavedImages();
。
如果您更新界面,则可以向PhotoChooserWithCameraServiceShow
方法添加事件处理程序:
public interface IImageService
{
ObservableCollection<object> RefreshSavedImages();
void PhotoChooserWithCameraServiceShow(EventHandler<PhotoResult> CompletedCallback);
}
然后在您的接口实现中,将该回调分配给PhotoChooserTask Completed事件。我假设你在这堂课中宣布你的PhotoChooserTask:
public class ImageService: IImageService
{
PhotoChooserTask photoChooserTask;
public ImageService()
{
photoChooserTask = new PhotoChooserTask();
}
public ObservableCollection<PictureModel> RefreshSavedImages()
{
// Refreash images from library
}
public WriteableBitmap StreamToWriteableBitmap(Stream imageStream)
{
....
}
public void PhotoChooserWithCameraServiceShowEventHandler<PhotoResult> CompletedCallback)
{
// PhotoChooserTask get source of image.
photoChooserTask.Completed += CompletedCallback;
}
}
最后,在您的视图模型中,您可以实现实际的回调:
private void MethodA()
{
// Choose Photo, I edited it and after saved it.
imageService.PhotoChooserWithCameraServiceShow(photoChooserTask_Completed);
}
// this is your callback
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
// Refreash the library and Bind it in my ItemSource from my ListBox.
PictureList = imageService.RefreshSavedImages();
}
}
请参阅详细信息和示例here。