我不确定如何在 .Net Maui 中实现 IExecutor。我需要这个执行器以便能够简单地在 Android 中运行一个函数(它接受 IExecutor 作为参数)。我真的不知道如何实现所有功能,但是这个对象必须实现 IExecutor...
#if ANDROID
using Java.Interop;
using Java.Lang;
using Java.Util.Concurrent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CellularSignal1
{
public class Executor : Java.Lang.Object, IExecutor
{
public nint Handle => throw new NotImplementedException();
public int JniIdentityHashCode => throw new NotImplementedException();
public JniObjectReference PeerReference => new JniObjectReference();
public JniPeerMembers JniPeerMembers => throw new NotImplementedException();
public JniManagedPeerStates JniManagedPeerState => throw new NotImplementedException();
public void Dispose()
{
throw new NotImplementedException();
}
public void Disposed()
{
throw new NotImplementedException();
}
public void DisposeUnlessReferenced()
{
throw new NotImplementedException();
}
public void Execute(IRunnable command)
{
command.Run();
}
public void Finalized()
{
throw new NotImplementedException();
}
public void SetJniIdentityHashCode(int value)
{
throw new NotImplementedException();
}
public void SetJniManagedPeerState(JniManagedPeerStates value)
{
throw new NotImplementedException();
}
public void SetPeerReference(JniObjectReference reference)
{
throw new NotImplementedException();
}
public void UnregisterFromRuntime()
{
throw new NotImplementedException();
}
}
}
#endif
我尝试按原样离开,但开始从中得到一堆“未实现”的异常
只需使用您的上下文(Android.Content.Context)“Executor”或“MainExecutor”即可。
例如,您可以从 MainActivity 中获取主执行器,如下所示
public class MainActivity : ...
{
public IExecutor GetExecutor() { return this.MainExecutor; }
}
顺便说一句,您不必重新实现 Java.Lang.Object 的属性。
有多种方法可以获取它。
API >= 28.0
Platform.CurrentActivity.MainExecutor
或
Platform.AppContext.MainExecutor
还有api < 28.0
ContextCompat.GetMainExecutor(Platform.AppContext)