Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a method located inside the Android project from the Portable Class Library Project in Xamarin.Forms?

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

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.

like image 790
Moji Avatar asked Jan 03 '15 13:01

Moji


People also ask

What is the difference between the portable class library and shared projects?

The difference between a shared project and a class library is that the latter is compiled and the unit of reuse is the assembly. Whereas with the former, the unit of reuse is the source code, and the shared code is incorporated into each assembly that references the shared project.

What is a portable class library?

Portable class libraries It is a class library that you write and can use in applications that run on multiple platforms. Their purpose is to share code between applications, just like . NET Standard does.

What is the difference between PCL & _shared project?

Shared Projects work well for application developers planning to use lots of platform-specific functionality in their cross-platform apps. While PCL projects continue to be supported in Visual Studio, . NET Standard is recommended for new projects.


1 Answers

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/

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:

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

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

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

like image 143
Vincent Poirier Avatar answered Sep 26 '22 17:09

Vincent Poirier