greenrobot / EventBus

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.
http://greenrobot.org/eventbus/
Apache License 2.0
24.66k stars 4.66k forks source link

How to use @Subscribe annotation on Xamarin.Forms Android platform using Greenrobot.EventBus package #661

Closed ZahraW25 closed 2 years ago

ZahraW25 commented 3 years ago

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);
      }
    }
greenrobot-team commented 3 years ago

EventBus is a Java/Kotlin library. I'm not sure how it would work with C#, which is the language above. No?

andob commented 3 years ago

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);
        }
wangyankun33 commented 2 years ago

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.
greenrobot-team commented 2 years ago

@wangyankun33 Sorry, I can't really help. I have no experience with Xamarin projects. Try asking e.g. on Stack Overflow?

wangyankun33 commented 2 years ago

@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!

andob commented 2 years ago

@wangyankun33 Try using a C# written EventBus library, for instance this one or this one. You can search for libraries on nuget.org by typing eventbus or messenger.

greenrobot-team commented 2 years ago

Alright, closing this then as alternatives are available.