相关文章推荐
非常酷的葡萄  ·  C++: ...·  6 月前    · 
不敢表白的小摩托  ·  Microsoft ...·  1 年前    · 
酒量大的面包  ·  在 Azure Cosmos DB ...·  1 年前    · 

Xamarin Forms -> System.MissingMethodException。没有找到[界面]的默认构造函数

1 人关注

我在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类型找到默认构造函数。

是什么出了问题?

3 个评论
如果你只是给你的TextRecognition类添加一个空的构造函数会怎么样?
@某个学生 什么都没有...同样的错误
我的方案对你有用吗?如果是,请您接受(点击本答案左上角的☑️),以便我们能够帮助更多有同样问题的人:)。
c#
android
xamarin
xamarin.forms
xamarin.android
João Godinho
João Godinho
发布于 2020-09-16
2 个回答
Jason
Jason
发布于 2020-09-17
0 人赞同

这正是错误所说的情况。 对于 DependencyService 来说,要实例化一个类,该类 MUST 有一个默认的(空)构造函数。 你的没有。

这就是上面SomeStudent给我的解决方案......它不起作用 :( 我更新了我的代码
你为什么要在FoodMenu中调用Register? FoodMenu不是ITextRecognition的实现类。
我以为我必须调用它......但我删除了,它给出了同样的错误。
你的 [Assembly] 属性的位置也不对--它属于实现接口的类。
nevermore
nevermore
发布于 2020-09-17
0 人赞同

你不需要在 Android 项目中创建一个新的 ITextRecognition 接口,你应该使用你在 Xamarin.forms 项目中创建的接口。

另外,最好不要在FoodMenu类中创建接口。

以下是代码示例。

在Xamarin.Forms。

namespace App384
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class FoodMenu : ContentPage
        public FoodMenu()
            InitializeComponent();
        public void Food_CameraEntry(object sender, EventArgs e)
            Xamarin.Forms.DependencyService.Register<ITextRecognition>();
            DependencyService.Get<ITextRecognition>().LaunchActivityInAndroid();
    public interface ITextRecognition
        void LaunchActivityInAndroid();

在Xamarin.Android项目中。

[assembly: Xamarin.Forms.Dependency(typeof(TextRecognition))]
namespace App384.Droid
    [Preserve(AllMembers = true)]
    [Activity(Label = "TextRecognition", Theme = "@style/Theme.AppCompat.Light.NoActionBar", MainLauncher = true)]
    public class TextRecognition : AppCompatActivity, ITextRecognition
        protected override void OnCreate(Bundle savedInstanceState)
            base.OnCreate(savedInstanceState);
        public void LaunchActivityInAndroid()
            Console.WriteLine("LaunchActivityInAndroid");