在 WP8 中依次调用两个接口方法



如果我有这样的接口:

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

当我在视图模型中实现时:

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();

问题是我的程序所做的是在第一个完成之前执行第二个。

我认为可能导致此问题的问题:

  1. 从 ViewModel 调用导航服务,无需逻辑即可返回。所以不能:

    导航

    服务导航 = 新的导航服务();导航。NavigateTo(new Uri("/Views/SecondPage.xaml", UriKind.Relative));

PhotoChooserWithCameraServiceShow取自Cimbalino Windows Phone Toolkit

谢谢大家,问候!

您需要为照片选择器任务的已完成事件设置事件侦听器,并从那里调用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();
        }
    }

请参阅此处的详细信息和示例。

相关内容

  • 没有找到相关文章

最新更新