Passing string/method form MainPage to MainActivity Xamarin
<p>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 </p>

<pre><code> DependencyService.Get&lt;IHPost&gt;.HPost("R");
</code></pre>

<p><strong>Method not valid in the given context</strong></p>

<p>I followed the official documentation,so I created my interface in the PCL:</p>

<p>IHPost (public interface)</p>

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

<p>and in MainActivity i added this interface.</p>

<p>Code : </p>

<p>IHPost.cs (Interface)</p>

<pre><code>    namespace Xam
{
    public interface IHPost
    {
        void HPost(string val);
    }
}
</code></pre>

<p>MainPage.xaml.cs</p>

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

<p>HPost_droid (Class)</p>

<pre><code>[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("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" + val);
        }
    }
}
</code></pre>

<p>MainActivity</p>

<pre><code>[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&lt;IHPost&gt;.HPost("R");

            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}
</code></pre>