【问题标题】:EventBus Xamaring. Custom method of activity not found using reflection事件总线 Xamaring。使用反射未找到自定义活动方法
【发布时间】:2021-09-04 10:17:30
【问题描述】:

我在我的 Xamarin Android 项目中添加了 EventBus 作为绑定库。 我可以编译没有错误,但是当我尝试注册时,我收到以下错误:

这是活动的代码

[Activity(Icon = "@mipmap/ic_launcher"]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Forms.Init(this, savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        CrossCurrentActivity.Current.Init(this, savedInstanceState);
        LoadApplication(new App());
        

        EventBus.Default.Register(this);
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        EventBus.Default.Unregister(this);
    }

    [Subscribe]
    public void onEvent(DeviceConnectionEvent my_event)
    {
        throw new NotImplementedException();
    }

    protected override void OnActivityResult(int requestCode, Result resultVal, Intent data)
    {

        if (requestCode == 1)
        {
            wifiManagerInstance.onNetworkDataReceived();
        }

        base.OnActivityResult(requestCode, resultVal, data);

    }
}

当 eventbus 使用反射时,我的方法似乎没有被检测到

methods = findState.clazz.getDeclaredMethods();

检测活动的其他方法如下图所示。

有什么我可以做的吗?

提前致谢。

【问题讨论】:

    标签: xamarin reflection binding event-bus


    【解决方案1】:

    由于我没有EventBus 的绑定库,我无法对其进行测试,但是 您可以在使用[Subscribe] 属性时检查您的包是否正确导入。

    这里你应该使用org.greenrobot.eventbus.Subscribe;而不是com.google.common.eventbus.Subscribe;

    我想你也可以使用MessagingCenter 来做你想做的事。

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2021-09-26
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多