我在Xamarin.Forms上使用依赖服务时收到了这个错误。我已经看到了关于这个错误的答案,其中涉及到iOS和链接器。然而,我是在安卓上运行的,链接器是关闭的。
构造函数告诉我,它在PCL中找不到接口的默认构造函数。
我已经搜索和调试了几个小时。有哪些事情可能导致这个错误?我非常确定我的DependencyService实现是正确的,所以我觉得这是一个不同的问题。
以下是我的相关代码。
[assembly: Xamarin.Forms.Dependency(typeof(TextRecognition))]
namespace DiabetesAPP.Droid
[Preserve(AllMembers = true)]
[Activity(Label = "TextRecognition", Theme = "@style/Theme.AppCompat.Light.NoActionBar", MainLauncher = true)]
public class TextRecognition : AppCompatActivity, ISurfaceHolderCallback, IProcessor, ITextRecognition
private SurfaceView cameraView;
private TextView textView;
private CameraSource cameraSource;
public string Resultados;
private const int RequestCameraPermissionID = 1001;
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
cameraView = FindViewById<SurfaceView>(Resource.Id.surface_view);
textView = FindViewById<TextView>(Resource.Id.txtview);
TextRecognizer textRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();
if (!textRecognizer.IsOperational)
Log.Error("Main Activity", "Detector dependencies are not yet available");
cameraSource = new CameraSource.Builder(ApplicationContext, textRecognizer)
.SetFacing(CameraFacing.Back)
.SetRequestedFps(2.0f)
.SetRequestedPreviewSize(1280, 1024)
.SetAutoFocusEnabled(true)
.Build();
cameraView.Holder.AddCallback(this);
textRecognizer.SetProcessor(this);
Android.Widget.Button logonButton = FindViewById<Android.Widget.Button>(Resource.Id.button_send);
logonButton.Click += LogonButton_Click;
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
switch (requestCode)
case RequestCameraPermissionID:
if (grantResults[0] == Android.Content.PM.Permission.Granted)
cameraSource.Start(cameraView.Holder);
break;
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
public void SurfaceCreated(ISurfaceHolder holder)
if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
ActivityCompat.RequestPermissions(this, new string[]
Android.Manifest.Permission.Camera
}, RequestCameraPermissionID);
return;
cameraSource.Start(cameraView.Holder);
public void SurfaceDestroyed(ISurfaceHolder holder)
cameraSource.Stop();
public void ReceiveDetections(Detections detections)
SparseArray items = detections.DetectedItems;
if (items.Size() != 0)
textView.Post(() =>
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < items.Size(); i++)
strBuilder.Append(((TextBlock)items.ValueAt(i)).Value);
strBuilder.Append("\n");
textView.Text = strBuilder.ToString();
Resultados = strBuilder.ToString();
private void LogonButton_Click(object sender, EventArgs e)
//Toast.MakeText(this, "Hello from " + Resultados, ToastLength.Long).Show();
//Intent data = new Intent(this, typeof(TextRecognition));
//SetResult(Result.Ok, data);
//// MessagingCenter.Send((DiabetesAPP.App)Xamarin.Forms.Application.Current, "OpenPage", "You send message:" + Resultados);
//// MessagingCenter.Send<DiabetesAPP.App, string>(DiabetesAPP.App.Current as App, "OpenPage", "You send message:" + Resultados);
//MessagingCenter.Send((DiabetesAPP.App)Xamarin.Forms.Application.Current, "OpenPage", "You send message:" + Resultados);
Toast.MakeText(this, Resultados, ToastLength.Short).Show();
MessagingCenter.Send<App, string>(App.Current as App, "OpenPage", "You send message:" + Resultados);
Finish();
public void Release()
public void LaunchActivityInAndroid()
Activity activity = Forms.Context as Activity;
var intent = new Intent(Forms.Context, typeof(TextRecognition));
activity.StartActivity(intent);
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
base.OnActivityResult(requestCode, resultCode, data);
switch (resultCode)
case Result.Ok:
break;
Finish();
public interface ITextRecognition
//Update code - 16/09/2020
public TextRecognition()
在Xamarin.Form页面中的实现
[assembly: Xamarin.Forms.Dependency(typeof(ITextRecognition))]
namespace DiabetesAPP.Views.FoodMenu
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class FoodMenu : ContentPage
public FoodMenu()
InitializeComponent();
async void Food_ManualEntry(object sender, EventArgs e)
await Navigation.PushAsync(new SearchFood());
public void Food_CameraEntry(object sender, EventArgs e)
Xamarin.Forms.DependencyService.Register<ITextRecognition>();
DependencyService.Get<ITextRecognition>().LaunchActivityInAndroid();
public interface ITextRecognition
void LaunchActivityInAndroid();
错误。System.MissingMethodException。没有为DiabetesAPP.Views.FoodMenu.FoodMenu+ITextRecognition类型找到默认构造函数。
是什么出了问题?