如何在我的共享代码中调用依赖注入截图代码?

时间:2017-10-10 14:04:37

标签: c# xamarin.forms screenshot

我是Xamarin.Forms的初学者,我有一个问题:如何在我的共享代码/ xaml.cs中调用以下代码?

public class screenshotManager : IScreenshotManager
{    

    public static Activity Activity { get; set; }

    public async System.Threading.Tasks.Task<byte[]> CaptureAsync()
    {
        if (Activity == null)
        {
            throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
        }

        var view = Activity.Window.DecorView;
        view.DrawingCacheEnabled = true;

        Bitmap bitmap = view.GetDrawingCache(true);

        byte[] bitmapData;

        using (var stream = new MemoryStream())
        {
            bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }

        return bitmapData;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您需要从字节[]中获取图像,正如我们在评论中所讨论的那样,那么您应该像@Jason所说的那样。

这样的事情:

var imageBytes = await screenshotManager.CaptureAsync();
Image imageView = new Image();
imageView.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));