简体   繁体   中英

How to make MainPage.xaml.cs talk to MainActivity.cs using interface

I'm trying to do these classes share some data, I read that the right way to do that is through an interface but MainPage.xaml.cs doesn't recognizes the "link". What I need to add in MainPage.xaml.cs to make it work?

Class1.cs:

namespace interfaceWebnav
{
    public interface IPortableInterface
    {
        object Test();
    }
}

MediaService.cs

using interfaceWebnav;

namespace ComprarMusicas
{
    public class MediaService : Java.Lang.Object, IPortableInterface
    {
        public object Test()
        {
            //do something
        }
    }
}

MainActivity.cs:

...
...
using interfaceWebnav;

[assembly: Dependency(typeof(IPortableInterface))]
namespace MyApp.Droid
{
     [Activity(Label = "Comprar Músicas", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, NoHistory = true,ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity :  global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ............
            base.OnCreate(savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
   }

    public override void OnBackPressed()
    {
        // here I need to know what SomeMethod() from MainPage.xaml.cs is saying
    }

}

MainPage.xaml.cs (I commented out some of my tries of make it work):

using Xamarin.Forms;
// using interfaceWebnav;

// [assembly: Dependency(typeof(IPortableInterface))]

namespace MyApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

    void SomeMethod(object sender, EventArgs e)
    {
        object resultado = DependencyService.Get<IPortableInterface>().Test();
    }
}

At MainPage.xaml.cs, visual studio says about SomeMethod: "A namespace can't contain members as fields or methods directly."

About "Get IPortableInterface" it says:

"The name of type or namespace IPortableInterface can't be find(are you missing an using directive or an assembly referency?)"

Add Class says MediaService.cs in your android project inherit it from IPortableInterface

public class MediaService : Java.Lang.Object, IPortableInterface
{
    public void Test()
    {
        // your android platform specific implementation
    }

}

In the MainPage.cs file your invoking dependency service

namespace MyApp
{
    public partial class MainPage : ContentPage
    {
        public bool foo = true; //just an example

        public MainPage()
        {
            InitializeComponent();
        }
    }
    //I suppose invoking any event lets says button click calling interface dependency service of android project
    void Button_Clicked(object sender,EventArgs e)
    {
        object resultado = DependencyService.Get<IPortableInterface>().Test();
    }

}

I hope this will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM