2

我正在使用 MvvmCross 框架开发我的第一个 Windows 应用商店应用程序,但我遇到了图像管理问题。特别是,我的 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 看来并不明显”。以前有人看过吗?谢谢

4

2 回答 2

1

这个未实现的异常记录在 wiki 中 - 请参阅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

于 2013-11-10T14:42:32.010 回答
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");
    }

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

于 2013-11-12T06:57:27.213 回答