我正在使用 MvvmCross 框架开发我的第一个 Windows 应用商店应用程序,但我在图像管理方面遇到问题。特别是,我的 PCL 项目中有以下简单的 ViewModel,以及一个带有与 AddPictureCommand 绑定(bind)的按钮的 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 来看似乎并不明显”。 有人以前看过吗? 谢谢
最佳答案
这个未实现的异常记录在 wiki 中 - 请参阅 https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#File
如果需要的话,实现这些缺失的方法应该相当简单。事实上,我知道至少有 2 位用户已经实现了这些 - 但遗憾的是他们还没有做出贡献。
要实现它们,只需
- 从 https://github.com/MvvmCross/MvvmCross/blob/v3/Plugins/Cirrious/File/Cirrious.MvvmCross.Plugins.File.WindowsStore/MvxWindowsStoreBlockingFileStore.cs fork (复制)代码
- 使用 winrt StorageFolder API 实现缺少的方法
- 在您的 Store UI 项目中,不要加载
File
插件 - 因此请注释掉或删除File
引导类。 在设置过程中,使用
Mvx.RegisterType
向 ioc 注册您的实现 - 例如:protected override void InitializeFirstChance() { base.InitializeFirstChance(); Cirrious.CrossCore.Mvx.RegisterType<IMvxFileStore, MyFileStore>(); }
有关使用 ioc 的更多信息,请参阅 https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control
有关自定义设置顺序的更多信息,请参阅 https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup
关于windows-store-apps - MvvmCross:NotImplementedException 调用 IMvxFileStore 的 EnsureFolderExists 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890756/