connect xamarin forms click method to android method

hossein tavakoli 471 Reputation points
2021-06-02T16:54:58.073+00:00

I use Camera2forms

I have a button in MainPage.xaml , also I have a method called "OpenCamera(...)" in Camera.Droid.

How can I Call "OpenCamera(...)" method from MainPage.xaml.

picture

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-06-03T03:03:09.167+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Yes, if you want to invoke native platform functionality in forms, you can use Xamarin.Forms DependencyService to achieve this function just @Alessandro Caliaro said.

    The DependencyService class is a service locator that enables Xamarin.Forms applications to invoke native platform functionality from shared code.

    The process for using the DependencyService to invoke native platform functionality is to:

    1. Create an interface for the native platform functionality, in shared code. For more information, see Create an interface.
    2. Implement the interface in the required platform projects. For more information, see Implement the interface on each platform.
    3. Register the platform implementations with the DependencyService. This enables Xamarin.Forms to locate the platform implementations at runtime. For more information, see Register the platform implementations.
    4. Resolve the platform implementations from shared code, and invoke them. For more information, see Resolve the platform implementations.

    And there is a sample included in above document, you can check it here: https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/dependencyservice/

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,196 Reputation points
    2021-06-02T19:08:55.303+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.