简体   繁体   English

如何使用interface使MainPage.xaml.cs与MainActivity.cs对话

[英]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". 我正在尝试将这些类共享一些数据,我读到正确的方法是通过接口,但MainPage.xaml.cs无法识别“链接”。 What I need to add in MainPage.xaml.cs to make it work? 我需要在MainPage.xaml.cs中添加什么才能使其正常工作?

Class1.cs: 将Class1.cs:

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

MediaService.cs MediaService.cs

using interfaceWebnav;

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

MainActivity.cs: 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): MainPage.xaml.cs(我注释了一些让它工作的尝试):

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." 在MainPage.xaml.cs上,visual studio讲述了SomeMethod:“命名空间不能直接包含成员作为字段或方法。”

About "Get IPortableInterface" it says: 关于“Get IPortableInterface”它说:

"The name of type or namespace IPortableInterface can't be find(are you missing an using directive or an assembly referency?)" “无法找到类型或命名空间IPortableInterface的名称(您是否缺少using指令或程序集引用?)”

Add Class says MediaService.cs in your android project inherit it from IPortableInterface Add Class说你的android项目中的MediaService.cs从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 在MainPage.cs文件中调用依赖项服务

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 我希望这能帮到您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Visual Studio 2017 中的 MainActivity.cs 访问 MainPage.xaml 中的 textview - How to access textview in MainPage.xaml from MainActivity.cs in visual studio 2017 如何在 Xamarin 表单中将 MainPage.xaml 中条目的文本属性访问到 MainActivity.cs? - How do I access the text property of an Entry in MainPage.xaml to MainActivity.cs in Xamarin forms? 类在MainPage.xaml.cs上实例化2次 - Class instantiate 2 times on MainPage.xaml.cs 如何从MainPage.xaml.cs调用LoadData() - How to call LoadData() from MainPage.xaml.cs 无法初始化MainPage.xaml.cs中的AudioGraph并无法通过MainPage.xaml.cs上的滑块设置频率 - Unable to initialize an AudioGraph in MainPage.xaml.cs and set frequency with a slider on MainPage.xaml.cs 如何从App.xaml.cs调用MainPage.xaml.cs中的函数 - How to call a function that's in MainPage.xaml.cs from App.xaml.cs 从phonegap应用程序的MainPage.xaml.cs切换html页面 - Switching the html page from the MainPage.xaml.cs of a phonegap application 将 ToolbarItems 添加到 Xamarin.Forms 项目中的 MainPage.xaml.cs - Adding ToolbarItems to MainPage.xaml.cs in a Xamarin.Forms Project 在 MainPage.xaml.cs 中使用 Android 特定代码 - Use Android specific code in MainPage.xaml.cs 如何在MainPage.xaml.cs的构造函数中将事件处理程序注册到另一个类的事件 - How to register a event handler to the event of another class in constructor of MainPage.xaml.cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM