如何从Xamarin.Forms中的可移植类库项目中调用位于Android项目内的方法?

问题描述:

这听起来可能是一个愚蠢的问题,但是由于我对Xamarin还是很陌生,所以我会继续努力.

This might sound a silly question but since I'm quite new to Xamarin, I'll go for it.

因此,我有一个Xamarin.Forms解决方案,并且有一个Android项目以及一个可移植类库.我正在从Android项目内的MainActivity.cs调用起始页面,该项目本身从可移植类库项目中定义的表单中调用了第一页(通过调用App.GetMainPage()).现在,我想在我的一个表单上添加一个click事件,以获取设备的当前位置.显然,要获取位置,我必须在Android项目中实现它.那么,如何在可移植类库项目中的click事件中调用GetLocation方法?任何帮助,将不胜感激.抱歉,可能会重复.

So I have a Xamarin.Forms solution and there's an Android project plus a Portable Class Library. I am calling the starting page from the MainActivity.cs inside the Android project which itself calls the first page from the forms defined in Portable Class Library project (by calling App.GetMainPage()). Now, I want to add a click event on one of my forms to get the current location of device. Apparently, to get the location I have to implement it inside the Android project. So how can I call the GetLocation method from my click event inside the Portable Class Library project? Any help would be appreciated. Sorry for possible duplicate.

如果您使用Xamarin.Forms.Labs,则该解决方案确实在提供的链接中.如果仅使用Xamarin.Forms,则几乎与使用DependencyService要做的一样.它比看起来容易. http://developer.xamarin.com/guides/cross-平台/xamarin-forms/dependency-service/

The solution is indeed in the provided link if you use Xamarin.Forms.Labs. If you only use Xamarin.Forms, it's almost the same thing you gotta do by using the DependencyService. It's easier than it seems. http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

我建议您阅读这篇文章,在那儿我几乎无法理解我的大脑. http://forums.xamarin.com/discussion/comment/95717

I suggest reading this post where I almost broke my brain trying to understand. http://forums.xamarin.com/discussion/comment/95717

为方便起见,这是一个工作示例,如果您尚未完成工作,则可以对其进行调整:

For convenience, here's a working example that you could adapt if you didn't already finish your work:

在Xamarin.Forms项目中创建一个界面.

Create an interface in your Xamarin.Forms project.

using Klaim.Interfaces;
using Xamarin.Forms;

namespace Klaim.Interfaces
{
    public interface IImageResizer
    {
        byte[] ResizeImage (byte[] imageData, float width, float height);
    }
}

在您的Android项目中创建服务/自定义渲染器.

Create the service/custom renderer in your Android project.

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 ();
            }
        }
    }
}

因此,当您呼叫此电话时:

So when you call this:

byte[] test = DependencyService.Get<IImageResizer>().ResizeImage(AByteArrayHereCauseFun, 400, 400);

它执行Android代码并将该值返回到您的Forms项目中.

It executes Android code and returns the value into your Forms project.