需要的例子拍照与MonoDroid的和MVVMCross(Need an example of ta

2019-07-03 23:58发布

任何人都可以点我的例子拍照并存储使用MVVMCross

我一直在寻找,但只找到了这个:

  • MonoDroid的,要考虑与相机拍摄照片 (没有实现MVVMCross)

  • 视频录制 (这是视频,我不能让它工作:S)

  • 该Oficialy配方实例 (它的作品,但没有实现MVVMCross)

谢谢!!!

解决! 谢谢!
未来的参考文献:(使用主科)
积分斯图尔特,我只是改变了代码与我的现实工作

using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Platform.Tasks;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using SIGEP.DummyService;
using SIGEP.Mobile.Core.Interfaces;


namespace SIGEP.Mobile.Core.Models
{
 public class PhotoService : IMvxServiceConsumer<IMvxPictureChooserTask>
{
    private const int MaxPixelDimension = 1024;
    private const int DefaultJpegQuality = 92;

    public void GetNewPhoto()
    {
        this.GetService<IMvxPictureChooserTask>().TakePicture(
            MaxPixelDimension,
            DefaultJpegQuality,
            HandlePhotoAvailable,
            () => { /* cancel is ignored */ });

    }

    public event EventHandler<PhotoStreamEventArgs> PhotoStreamAvailable;

    private void HandlePhotoAvailable(Stream pictureStream)
    {
        var handler = PhotoStreamAvailable;
        if (handler != null)
        {
            handler(this, new PhotoStreamEventArgs() { PictureStream = pictureStream, OnSucessGettingPhotoFileName = OnSucessGettingPhotoFileName });
        }
    }



    public static void TakePhoto(Action<string> successFileName, Action<Exception> error)
    {
        var service = new PhotoService();
        service.OnSucessGettingPhotoFileName = successFileName;
        service.OnError = error;
        service.GetNewPhoto();
        service.PhotoStreamAvailable += new EventHandler<PhotoStreamEventArgs>(service_PhotoStreamAvailable);
    }

    static void service_PhotoStreamAvailable(object sender, PhotoStreamEventArgs e)
    {
        //grava pra ficheiro!!!
        var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var filename = Path.Combine(directory, "photo.jpeg");
        string saveTo = filename;
        FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
        ReadWriteStream(e.PictureStream, writeStream);

        e.OnSucessGettingPhotoFileName(filename);

    }
    private static void ReadWriteStream(Stream readStream, Stream writeStream)
    {
        int Length = 256;
        Byte[] buffer = new Byte[Length];
        int bytesRead = readStream.Read(buffer, 0, Length);
        // write the required bytes
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = readStream.Read(buffer, 0, Length);
        }
        readStream.Close();
        writeStream.Close();
    }

    public Action<string> OnSucessGettingPhotoFileName { get; set; }
    public Action<Exception> OnError { get; set; }
}

[Serializable]
[ComVisible(true)]
public class PhotoStreamEventArgs : EventArgs
{
    public Stream PictureStream { get; set; }

    public Action<string> OnSucessGettingPhotoFileName { get; set; }
}
}

Answer 1:

我一般使用实现服务内置IMvxPictureChooserTask (这是如果使用vNext一个插件):

using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Platform.Tasks;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;

public class PhotoService 
    : IMvxServiceConsumer<IMvxPictureChooserTask>
    , IPhotoService
{
    private const int MaxPixelDimension = 1024;
    private const int DefaultJpegQuality = 92;

    public void GetNewPhoto()
    {
        Trace.Info("Get a new photo started.");

        this.GetService<IMvxPictureChooserTask>().TakePicture(
            MaxPixelDimension,
            DefaultJpegQuality,
            HandlePhotoAvailable,
            () => { /* cancel is ignored */ });
    }

    public event EventHandler<PhotoStreamEventArgs> PhotoStreamAvailable;

    private void HandlePhotoAvailable(Stream pictureStream)
    {
        Trace.Info("Picture available");
        var handler = PhotoStreamAvailable;
        if (handler != null)
        {
            handler(this, new PhotoStreamEventArgs() { PictureStream = pictureStream });
        }
    }
}

我一般注册该服务在启动时一个单,然后从一个视图模型的ICommand处理程序调用它。


使用这种服务的一个应用是Blooor样本-见BaseEditProductViewModel.cs -这是不是一个样我有什么做的,但我相信它带来了两个拍照和斑马线-无论是使用外部服务。


一个警告:在MonoDroid的,你可以看到一些奇怪的/出乎意料的活动/视图模型的生命周期行为 - 基本上你可以看到,活动你从被卸载照片/拍摄照片的过程中,从记忆中抹去了。 如果发生这种情况,您的应用程序,那么你可能需要开始寻找这样的问题: 使用保存实例状态保存Android的活动状态 -这不是自动MvvmCross(还)处理。

我相信Blooor样品可以从这个问题遭受 - 但用户是否会永远看到它在正常的应用程序使用是值得商榷的。


作为替代IMvxPictureChooserTask服务,您也可以看看使用一些从Xamarin.Mobile跨平台的API -见MvvmCross vnext:MonoDroid的使用VideoView插件中一个可能的起点-或为Android只有你可以很容易地实现自己的。



文章来源: Need an example of take a Picture with MonoDroid and MVVMCross