如果您使用Xamarin.Forms.Labs,解決方案確實在提供的鏈接中。如果你只使用Xamarin.Forms,它幾乎是你必須通過使用DependencyService來做同樣的事情。這比看起來更容易。 http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/
我建議讀這篇文章,我幾乎打破了我的大腦試圖理解。 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代碼,並返回值到您的表單項目。
對不起,它是重複的! http://stackoverflow.com/questions/25588557/xamarin-forms-get-data-from-device-specific-code-back-to-the-forms?rq=1 http://developer.xamarin.com /導遊/跨平臺/ xamarin表單/依存服務/ – Moji