Search code examples
windows-store-appsmvvmcross

MvvmCross: NotImplementedException calling EnsureFolderExists method of IMvxFileStore


I'm developing my first Windows Store App, using MvvmCross framework and I have a problem with images management. In particular I have the following simple ViewModel in my PCL project, and a Store project with a button bound with AddPictureCommand.

    public class FirstViewModel : MvxViewModel
{

    IMvxPictureChooserTask _pictureChooserTask;
    IMvxFileStore _fileStore;

    public FirstViewModel(IMvxPictureChooserTask pictureChooserTask, IMvxFileStore fileStore)
    {
        _pictureChooserTask = pictureChooserTask;
        _fileStore = fileStore;
    }

    private byte[] _pictureBytes;
    public byte[] PictureBytes
    {
        get { return _pictureBytes; }
        set
        {
            if (_pictureBytes == value) return;
            _pictureBytes = value;
            RaisePropertyChanged(() => PictureBytes);
        }
    }

    public ICommand AddPictureCommand
    {
        get { return new MvxCommand(() => 
        {
            _pictureChooserTask.ChoosePictureFromLibrary(400, 95, pictureAvailable, () => { });
        }); }
    }

    private void pictureAvailable(Stream stream)
    {
        MemoryStream memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        PictureBytes = memoryStream.ToArray();

        GenerateImagePath();
    }

    private string GenerateImagePath()
    {
        if (PictureBytes == null) return null;
        var RandomFileName = "Image" + Guid.NewGuid().ToString("N") + ".jpg";
        _fileStore.EnsureFolderExists("Images");
        var path = _fileStore.PathCombine("Images", RandomFileName);
        _fileStore.WriteFile(path, PictureBytes);

        return path;
    }
}

The problem is that the method _fileStore.EnsureFolderExists("Images"); gives me the an "NotImplementedException" with message: "Need to implement this - doesn't seem obvious from the StorageFolder API". Has anyone already seen it before? Thank you


Solution

  • This not implemented exception is documented in the wiki - see https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#File

    It should be fairly straightforward to implement these missing methods if they are required. Indeed I know of at least 2 users that have implemented these - but sadly they've not contributed them back.

    to implement them, just

    For more on using ioc, see https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control

    For more on customising the setup sequence, see https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup