user
user

Reputation: 1759

Invoke interface classes upon button click in Xamarin Forms

I have platform specific sdks to be implemented and using Dependency Service for this purpose.

Upon a button click, I want to invoke the native sdks, but not sure how this can be done. Here is what I have tried.

Main Page

private async void ButtonClickMethod(object sender, EventArgs e, string name)
{
    // InterfaceDemo if = DependencyService.Get<InterfaceDemo>().GetName()  ///Tried this, but fails.

    DependencyService.Get<InterfaceDemo>().GetName();   ///How can this be invoked on button click

}  

Interface class:

namespace Demo
{
    public interface InterfaceDemo
    {
      void GetName();

    }
}

Platform Specific code:

namespace Demo.Droid
{
     public partial class Demo : InterfaceDemo
    {

        public async void GetName()
        {
             /// code
        }
     }
 }

Upvotes: 0

Views: 111

Answers (1)

Nikhil
Nikhil

Reputation: 3387

What you have done is correct but you have missed one very important line of code i.e. registering the Dependency in your native assembly.

So the updated Platform Specific Code will have the following attribute:-

[assembly: Xamarin.Forms.Dependency(typeof(Demo))]
 namespace Demo.Droid
{
     public partial class Demo : InterfaceDemo
    {

        public async void GetName()
        {
             /// code
        }
     }
 }

Upvotes: 1

Related Questions