MvvmCross:调用IMvxFileStore的EnsureFolderExists方法的NotImplementedException

时间:2013-11-10 14:18:55

标签: windows-store-apps mvvmcross

我正在开发我的第一个Windows应用商店应用,使用MvvmCross框架,我遇到图像管理问题。特别是我在我的PCL项目中有以下简单的ViewModel,以及一个带有与AddPictureCommand绑定的按钮的Store项目。

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

问题是方法 _fileStore.EnsureFolderExists(“Images”); 给我一个带有消息的“NotImplementedException”:“需要实现它 - 从StorageFolder API看起来并不明显”。 有没有人以前见过它? 谢谢

2 个答案:

答案 0 :(得分:1)

维基中记录了这个未实现的异常 - 请参阅https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#File

如果需要,实现这些缺失的方法应该相当简单。事实上,我知道至少有2个用户已经实现了这些 - 但遗憾的是他们没有贡献回来。

实施它们,只是

有关使用ioc的更多信息,请参阅https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control

有关自定义设置顺序的详细信息,请参阅https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup

答案 1 :(得分:1)

根据Stuart的建议,我已经为Windows 8 Store App实现了以下方法:

        public bool FolderExists(string folderPath)
    {
        try
        {
            var directory = ToFullPath(folderPath);
            var storageFolder = StorageFolder.GetFolderFromPathAsync(directory).Await();
        }
        catch (FileNotFoundException)
        {
            return false;
        }
        catch (Exception ex)
        {
            MvxTrace.Trace("Exception in FolderExists - folderPath: {0} - {1}", folderPath, ex.ToLongString());
            throw ex;
        }
        return true;
        //throw new NotImplementedException("Need to implement this - See EnsureFolderExists");
    }

        public void EnsureFolderExists(string folderPath)
    {
        try
        {
            var directory = ToFullPath(folderPath);
            var storageFolder = StorageFolder.GetFolderFromPathAsync(directory).Await();
        }
        catch (FileNotFoundException)
        {
            var localFolder = ToFullPath(string.Empty);
            var storageFolder = StorageFolder.GetFolderFromPathAsync(localFolder).Await();
            storageFolder.CreateFolderAsync(folderPath).Await();
        }
        catch (Exception ex)
        {
            MvxTrace.Trace("Exception in EnsureFolderExists - folderPath: {0} - {1}", folderPath, ex.ToLongString());
            throw ex;
        }

        //throw new NotImplementedException("Need to implement this - doesn't seem obvious from the StorageFolder API");
        //var folder = StorageFolder.GetFolderFromPathAsync(ToFullPath(folderPath)).Await();
    }

我们需要实现的第三个方法是DeleteFolder(string folderPath,bool recursive)。不幸的是,StorageFolder方法“DeleteFolder”没有“递归”参数。所以我应该忽略它来实现DeleteFolder:

        public void DeleteFolder(string folderPath, bool recursive)
    {
        try
        {
            var directory = ToFullPath(folderPath);
            var storageFolder = StorageFolder.GetFolderFromPathAsync(directory).Await();
            storageFolder.DeleteAsync().Await();
        }
        catch (FileNotFoundException)
        {
            //Folder doesn't exist. Nothing to do
        }
        catch (Exception ex)
        {
            MvxTrace.Trace("Exception in DeleteFolder - folderPath: {0} - {1}", folderPath, ex.ToLongString());
            throw ex;
        }
        //throw new NotImplementedException("Need to implement this - See EnsureFolderExists");
    }

或者如果“recursive”等于false,我应该检查文件夹是否为空,然后再将其删除。 欢迎更好的实施。