0
votes

I want to use a variabele or method from my MainPage in my MainActivity. I have created an interface for my DependencyService but I always have the same error at

 DependencyService.Get<IHPost>.HPost("R");

Method not valid in the given context

I followed the official documentation,so I created my interface in the PCL:

IHPost (public interface)

In Xamarin.Android i have written the implementation HPost_droid with [assembly: Dependency(typeof(HPost_droid))] line

and in MainActivity i added this interface.

Code :

IHPost.cs (Interface)

    namespace Xam
{
    public interface IHPost
    {
        void HPost(string val);
    }
}

MainPage.xaml.cs

 public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
               // DependencyService.Get<IHPost>.HPost("R");
            }
        }

HPost_droid (Class)

[assembly: Dependency(typeof(HPost_droid))]
namespace Xam.Droid
{
    class HPost_droid : Java.Lang.Object, IHPost
    {
        public HPost_droid() { }

        public void HPost(string val)
        {
            System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + val);
        }
    }
}

MainActivity

[assembly: Xamarin.Forms.Dependency(typeof(IHPost))]

namespace Xam.Droid
{
    [Activity(Label = "Xam", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IHPost
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            DependencyService.Get<IHPost>.HPost("R");

            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}
1
Post some more code, post your DependencyService Class, where it's implemented and it's interfaces. I started Xamarin a year ago and faced this very issue and would like to help but elaborate your question please. And if you're doing dependency injection why not use MvvmLight or MvvmCross (or another mvvm framework / Ioc Container)? they're designed to take this work out of your hands. - SilentStorm
First off, you cannot use Xamarin Forms classes, such as DependencyService before calling Xamarin.Forms.Forms.Init(this, bundle); (since Init gets all of the Xamarin Forms classes available for use). Also, if you are already in your MainActivity, why not just run the following: new HPost_droid().HPost("R");? You do not need DependencyService when you are already in your native project. Maybe tell us what you are trying to accomplish with HPost() - hvaughan3
I want passing some data from MainPage to MainActivity for each platfrom like a field of an entry or other things like this or some events. - Sarcel

1 Answers

0
votes

I retry from scratch application following this perfect tutorial : https://wolfprogrammer.com/2016/08/05/dependency-services-with-xamarin-forms/ and it's work !