IObserver 缺少方法

IObserver is missing methods

Observable class 已经可以使用了,但是我仍在为 IObserver.

苦苦挣扎

在 'IObserver' 的 Xamarin 文档中说,我只需要为 interface IObserver.

实现 Update() 方法

我的问题:

  1. 当我添加 : IObserver 时出现错误,提示我没有实现这两个方法:

            public IntPtr Handle => throw new NotImplementedException();
    
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    

    为什么我需要这些方法,它们有什么用? Xamarin 文档中未提及它们。

  2. 当我添加这些方法时,我收到以下错误消息: 错误 XA4212:类型 firstTry3.bluetoothConnectionActivity/deviceFoundObserver 实现 Android.Runtime.IJavaObject 但不继承 Java.Lang.ObjectJava.Lang.Throwable。这是不支持的。

  3. 当我在更新方法中添加override时,编译器说没有合适的方法覆盖

这是我的代码:

        public class deviceFoundObserver : IObserver
    {
        bluetoothConnectionActivity mActivity;

        public deviceFoundObserver(bluetoothConnectionActivity a)
        {
            mActivity = a;
        }

        //function is called from subject observable whenever a change happened
        public override void Update(Observable observable, Java.Lang.Object data)       //perhaps arguments like this: Observable observable, Object data
        {
            //there could be other messages as well, so first check wheather it is the right call from observer
            if ((string)data == deviceName)
            {
                mActivity.buttonStartEKG.Visibility = ViewStates.Visible;
                Log.Info(constants.tag, MethodBase.GetCurrentMethod().Name + ": Observable called update function in Observer");
            }
        }
    }

我假设我在使用 Observer 的方式上有问题。谁能帮帮我?

Why do I need these methods and what are they for? They are not mentioned in the Xamarin documentation.

让我们看一下IObserver的定义:

namespace Java.Util
{
    public interface IObserver : IJavaObject, IDisposable
    {
        void Update(Observable o, Lang.Object arg);
    }
}

这两个方法这里就不说了,但是我们可以发现IObserver继承自IJavaObjectIDisposable。然后去定义IJavaObjectIDisposable

namespace Android.Runtime
{
    public interface IJavaObject : IDisposable
    {
        IntPtr Handle { get; }
    }
}

namespace System
{
    public interface IDisposable
    {
        void Dispose();
    }
}

我们可以在这里找到这两种方法。 实现接口的class或结构必须实现接口定义中指定的接口成员。您还应该实现[=14=中定义的方法] 和 IDisposable.

When I add these methods, I get the follwing error message: error XA4212:.....

添加这些方法后我没有得到这个错误。再试一次,你可以参考这些类似的问题:error-xa4212, implementing-java-interfaces.

When I add override to the update method, the compiler says that there is no suitable method to override

不应向更新方法添加覆盖。只需实现接口中定义的方法而不是重写。您可以在此处查看 interface definition