如果您使用 Xamarin.Forms.Labs,解决方案确实位于提供的链接中。如果您只使用 Xamarin.Forms,那么almost使用 DependencyService 也要做同样的事情。这比看起来容易。http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/ http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/
我建议阅读这篇文章,我几乎绞尽脑汁试图理解。http://forums.xamarin.com/discussion/comment/95717 http://forums.xamarin.com/discussion/comment/95717
为了方便起见,这里有一个工作示例,如果您尚未完成工作,您可以调整该示例:
在 Xamarin.Forms 项目中创建一个界面。
using Klaim.Interfaces;
using Xamarin.Forms;
namespace Klaim.Interfaces
{
public interface IImageResizer
{
byte[] ResizeImage (byte[] imageData, float width, float height);
}
}
在您的 Android 项目中创建服务/自定义渲染器。
using Android.App;
using Android.Graphics;
using Klaim.Interfaces;
using Klaim.Droid.Renderers;
using System;
using System.IO;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: Xamarin.Forms.Dependency (typeof (ImageResizer_Android))]
namespace Klaim.Droid.Renderers
{
public class ImageResizer_Android : IImageResizer
{
public ImageResizer_Android () {}
public byte[] ResizeImage (byte[] imageData, float width, float height)
{
// Load the bitmap
Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);
using (MemoryStream ms = new MemoryStream())
{
resizedImage.Compress (Bitmap.CompressFormat.Jpeg, 100, ms);
return ms.ToArray ();
}
}
}
}
所以当你这样调用时:
byte[] test = DependencyService.Get<IImageResizer>().ResizeImage(AByteArrayHereCauseFun, 400, 400);
它执行 Android 代码并将值返回到您的 Forms 项目中。