2

如果我有这样的接口:

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

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

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

  1. 从 ViewModel 调用 NavigationService 没有逻辑返回。所以不能:

    NavigationService 导航 = 新 NavigationService(); navigation.NavigateTo(new Uri("/Views/SecondPage.xaml", UriKind.Relative));

PhotoChooserWithCameraServiceShow 取自Cimbalino Windows Phone 工具包

谢谢大家和问候!

4

1 回答 1

2

You need to set up an event listener for the photo chooser task's completed event and call imageService.RefreshSavedImages(); from there.

If you update your interface, you can add an event handler to your PhotoChooserWithCameraServiceShow method:

public interface IImageService
{
    ObservableCollection<object> RefreshSavedImages();
    void PhotoChooserWithCameraServiceShow(EventHandler<PhotoResult> CompletedCallback);
}

Then in your interface implementation, assign that callback to the PhotoChooserTask Completed event. I'm assuming you're declaring your PhotoChooserTask in this class:

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

Finally, in your view model you can implement the actual callback:

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

See the details and examples here.

于 2014-02-27T17:25:43.647 回答