我有一个Xamarin Forms接口,它定义了一个蓝牙控制器。尝试创建特定于Android的实现时,我遵循的是通常的技术(当我可以使用此方法时,我还将做一个iOS)。
我的界面定义如下:
namespace ArduinoRobotController.Models
{
public interface BluetoothControllerInterface
{
List<string> GetPairedDevices();
..
然后我有这样的平台特定实现:
[assembly: Xamarin.Forms.Dependency(typeof(BluetoothControllerInterface))]
namespace ArduinoRobotController.Droid.Implementations
{
public class BluetoothController : BluetoothControllerInterface
{
..
然后终于回到我的一个视图模型中,我有了这段代码来获取所需的特定于平台的实例:
BluetoothControllerInterface bt = Xamarin.Forms.DependencyService.Get<BluetoothControllerInterface>();
它可以生成并运行,但是在上面的行中崩溃。错误状态:
System.MissingMethodException:在SystemRuntimeType.CreateInstanceMono处找不到类型为ArduinoRobotController.Models.BluetoothControllerInterface的默认构造函数
..等等
为此,我尝试了许多不同的方法,包括调用以普通代码而不是[assembly
的形式注册实现。对此非常有帮助。
答案 0 :(得分:3)
您的注册需要指向具体的类,而不是接口
[assembly: Xamarin.Forms.Dependency(typeof(BluetoothController))]