C# 如何使用接口使MainPage.xaml.cs与MainActivity.cs对话
我试图让这些类共享一些数据,我了解到正确的方法是通过接口,但MainPage.xaml.cs无法识别“链接”。我需要在MainPage.xaml.cs中添加什么才能使其正常工作 Class1.cs:C# 如何使用接口使MainPage.xaml.cs与MainActivity.cs对话,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我试图让这些类共享一些数据,我了解到正确的方法是通过接口,但MainPage.xaml.cs无法识别“链接”。我需要在MainPage.xaml.cs中添加什么才能使其正常工作 Class1.cs: namespace interfaceWebnav { public interface IPortableInterface { object Test(); } } MediaService.cs using interfaceWebnav; name
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(我对我的一些尝试进行了注释,以使其正常工作):
使用Xamarin.Forms;
//使用interfaceWebnav;
//[程序集:依赖项(typeof(IPortableInterface))]
名称空间MyApp
{
公共部分类主页:ContentPage
{
公共主页()
{
初始化组件();
}
}
void SomeMethod(对象发送方,事件参数e)
{
object resultado=DependencyService.Get().Test();
}
}
在MainPage.xaml.cs上,visual studio介绍了一些方法:
“命名空间不能直接包含作为字段或方法的成员。”
关于“Get IPortableInterface”,它说:
“找不到类型或命名空间IPortableInterface的名称(是否缺少using指令或程序集引用?)”android项目中的Add类表示MediaService.cs从IPortableInterface继承它
public class MediaService : Java.Lang.Object, IPortableInterface
{
public void Test()
{
// your android platform specific implementation
}
}
在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();
}
}
名称空间MyApp
{
公共部分类主页:ContentPage
{
public bool foo=true;//只是一个例子
公共主页()
{
初始化组件();
}
}
//我假设调用任何事件都可以让按钮点击调用android项目的接口依赖服务
已单击无效按钮(对象发送者,事件参数e)
{
object resultado=DependencyService.Get().Test();
}
}
我希望这将帮助您而不是调用MainActivity.cs将新类添加到您的android项目中,并从接口继承该类,然后使用MainPage.xaml.cs中的依赖项服务调用该类。问题标题和您的帖子完全不同您的
依赖项服务
以错误的方式编写,请参阅文档以了解更多详细信息。请看一下我所做的修改,并为结论提供一些帮助?@ÉderRochaBezerra现在有什么问题?我更新了这个问题,请看新的代码,现在的问题是MainPage.xaml.cs,visual studio谈到SomeMethod时说:“名称空间不能直接包含作为字段或方法的成员。”关于Get-IPortableInterface,它说:“找不到类型或名称空间IPortableInterface的名称(是否缺少using指令或程序集引用?)您正在显示的代码段似乎并不是导致错误的直接原因。这就是导致错误的原因?显示完整的代码。包括所有名称空间和直到最后一个花括号。我对这一点很新,实际上我只想将变量设置为静态,以便在其他视图上访问。这是解决方案,而不是接口LOL。
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();
}
}