How to use @Subscribe annotation on Xamarin.Forms Android platform using Greenrobot.EventBus package
ZahraW25 opened this issue · 7 comments
I am trying to implement EventBus on Xamarin but Could not able to Subscribe the Events because when I am adding the annotation @subscribe(), I am getting an error "@subscribe does not exist in the current context". And when I am running the Application without @subscribe annotation, it is showing me an Error saying "MainActivity and its super classes have no public-methods with the @subscribe annotation". Please help me on how to subscribe the Event on Xamarin Android platform
MainActivity.cs class:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
//Registering Event Bus here
EventBus.Default.Register(this);
SetContentView(Resource.Layout.activity_main);
}
@Subscribe //Showing me an error while adding this annotation
public void OnEvent(NetworkInfo networkInfo)
{
Console.WriteLine(networkInfo);
}
protected void OnDestroy()
{
base.OnDestroy();
EventBus.Default.Unregister(this);
}
}
#PublisherEvent.cs:
public class PublishEvent
{
public PublishEvent()
{
EventBus.getDefault().post(message);
}
}
EventBus is a Java/Kotlin library. I'm not sure how it would work with C#, which is the language above. No?
I guess Xamarin has C# <-> Java interop.
Try C#'s annotation ("attributes") syntax, [AnnotationName] instead of @ AnnotationName
[Subscribe]
public void OnEvent(NetworkInfo networkInfo)
{
Console.WriteLine(networkInfo);
}
I am trying to implement EventBus on Xamarin but Could not able to Subscribe the Events because when I am adding the annotation @subscribe(), I am getting an error "@subscribe does not exist in the current context". And when I am running the Application without @subscribe annotation, it is showing me an Error saying "MainActivity and its super classes have no public-methods with the @subscribe annotation". Please help me on how to subscribe the Event on Xamarin Android platform
MainActivity.cs class: public class MainActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set our view from the "main" layout resource //Registering Event Bus here EventBus.Default.Register(this); SetContentView(Resource.Layout.activity_main); } @Subscribe //Showing me an error while adding this annotation public void OnEvent(NetworkInfo networkInfo) { Console.WriteLine(networkInfo); } protected void OnDestroy() { base.OnDestroy(); EventBus.Default.Unregister(this); } } #PublisherEvent.cs: public class PublishEvent { public PublishEvent() { EventBus.getDefault().post(message); } }
protected override void OnStart()
{
base.OnStart();
EventBus.Default.Register(this);
}
protected override void OnStop()
{
base.OnStop();
EventBus.Default.Unregister(this);
}
[Subscribe]
public void OnEvent(MessageModel model)
{
model.msg.ToWriteLine();
}
Error Msg:
Subscriber class crc64a7a6b04b89628087.MainActivity and its super classes have no public methods with the @Subscribe annotation.
@wangyankun33 Sorry, I can't really help. I have no experience with Xamarin projects. Try asking e.g. on Stack Overflow?
@wangyankun33 Sorry, I can't really help. I have no experience with Xamarin projects. Try asking e.g. on Stack Overflow?
Thanks,I am changing the Android Library of eventbus to C # Library!
Alright, closing this then as alternatives are available.