如何使用 Media Plugin 在 Xamarin.Forms 中实现 Camera Overlay 的依赖服务?

How to Implement Dependency Service for Camera Overlay in Xamarin.Forms Using the Media Plugin?

我正在尝试使用 Xamarin 的媒体插件将相机覆盖功能作为依赖服务传递到我的共享代码中 https://github.com/jamesmontemagno/MediaPlugin

我不知道如何正确实现依赖服务。该应用程序运行但当我打开相机时,它不显示叠加层。如果有人可以帮助我编写代码,或者指导我使用覆盖选项的示例,我将不胜感激。

我的接口代码:

public interface IPhotoOverlay 
{
   object GetImageOverlayAsync();
}

我的iOS代码:

public object GetImageOverlayAsync()
    {
        Func<object> func = CreateOverlay;

        return func;
    }

    public object CreateOverlay()
    {
        var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
        imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

        var screen = UIScreen.MainScreen.Bounds;
        imageView.Frame = screen;

        return imageView;
    }

我的分享码:

var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() {
            OverlayViewProvider = DependencyService.Get<IPhotoOverlay>().GetImageOverlayAsync,
            DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front});

在你的Xamarin.iOS服务中,你需要注册依赖,这里是一个例子。

[assembly: Dependency (typeof (PhotoOverlayiOS))]
namespace UsingDependencyService.iOS
{
    public class PhotoOverlayiOS : IPhotoOverlay
    {

        public object GetImageOverlayAsync()
        {
            Func<object> func = CreateOverlay;

            return func;
        }

        public object CreateOverlay()
        {
            var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
            imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

            var screen = UIScreen.MainScreen.Bounds;
            imageView.Frame = screen;

            return imageView;
        }  
    }
}