简体   繁体   English

Xamarin 依赖服务

[英]Xamarin DependencyService

I followed other questions in stack overflow and made sure my register in the assembly registers the android implementation and not the base interface and also that all classes are public.我关注了堆栈溢出中的其他问题,并确保我在程序集中的寄存器注册了 android 实现而不是基本接口,并且所有类都是公共的。 Anyhow I still get the System.MissingMethodException: 'Default constructor not found for type Foodies.VisualEffects.IStatusBarColor' message.无论如何,我仍然收到System.MissingMethodException: 'Default constructor not found for type Foodies.VisualEffects.IStatusBarColor'消息。

I declare my base interface in the common project at Foodies/Views/VisualEffects/iStatusBarColor.cs, like this:我在 Foodies/Views/VisualEffects/iStatusBarColor.cs 的通用项目中声明了我的基本接口,如下所示:

namespace Foodies.Views.VisualEffects
{
    public interface IStatusBarColor
    {
        void MakeMe(string color);
    }
}

Then in my android project I add StatusBarColor_Android, looking like:然后在我的 android 项目中添加 StatusBarColor_Android,如下所示:

using Android.OS;
using Foodies.Droid;
using Foodies.Views.VisualEffects;
using Xamarin.Forms.Platform.Android;

[assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))]

namespace Foodies.Droid
{ 
    public class StatusBarColor_Android : IStatusBarColor
    {
        public void MakeMe(string color)
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                var c = MainActivity.context as FormsAppCompatActivity;
                c?.RunOnUiThread(() => c.Window.SetStatusBarColor(Android.Graphics.Color.ParseColor(color)));
            }
        }
    }
}

and them from my main page I call:他们从我的主页上调用:

 var dp = DependencyService.Get<IStatusBarColor>();
                dp?.MakeMe(Color.Blue.ToHex());

And there, i the DependencyService.Get I do get the System.MissingMethodException: 'Default constructor not found for type Foodies.VisualEffects.IStatusBarColor在那里,我是 DependencyService.Get 我确实得到了 System.MissingMethodException:'未找到类型 Foodies.VisualEffects.IStatusBarColor 的默认构造函数

This is my android project settings这是我的 android 项目设置安卓项目设置

Im new to Xamarin, could someone help me finding the error??我是 Xamarin 的新手,有人可以帮我找出错误吗?

The namespace in the code above is confusing,as you said正如你所说,上面代码中的namespace令人困惑

I declare my base interface in the common project at Foodies/Views/iStatusBarColor.cs我在 Foodies/Views/iStatusBarColor.cs 的通用项目中声明了我的基本接口

Normally if you don't customize namepace, it should be:通常如果不自定义namepace,应该是:

namespace Foodies.Views
{
   public interface IStatusBarColor
   {
      void MakeMe(string color);
   }
}

and in you Android project,why you have two different reference using Foodies.Droid;在您的 Android 项目中,为什么您using Foodies.Droid; and using Foodies.VisualEffects.Droid;using Foodies.VisualEffects.Droid; ,and you didn't reference the namepace Foodies.Views when you implement IStatusBarColor ,并且您在实现Foodies.Views时没有引用名称空间IStatusBarColor

Try to change like:尝试改变如下:

using Foodies.Droid;
using Foodies.Views;
using Android.OS;
using Xamarin.Forms.Platform.Android;

[assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))]

namespace Foodies.Droid
{ 
  public class StatusBarColor_Android : IStatusBarColor
   {
      public void MakeMe(string color)
      {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            var c = MainActivity.context as FormsAppCompatActivity;
            c?.RunOnUiThread(() => c.Window.SetStatusBarColor(Android.Graphics.Color.ParseColor(color)));
        }
      }
   }
}

Update : The cause of this problem is the wrong registration method.There are two ways to register.更新:这个问题的原因是注册方法错误。有两种注册方法。

1.Register in your implementation class directly,like the codes above using [assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))] 1.直接在你的实现class中注册,就像上面的代码一样使用[assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))]

2.call DependencyService.Register<StatusBarColor_Android >(); 2.调用DependencyService.Register<StatusBarColor_Android >(); in your MainActivity OnCreate method.( Note :here you should use the class name which you implement the interface,not the name of interface).在您的 MainActivity OnCreate方法中。(注意:这里您应该使用实现接口的 class 名称,而不是接口名称)。

Found out in addition to all of this, in the MainActivity, in the OnCreate I had除了所有这些,在 MainActivity 中,在我的OnCreate中发现

DependencyService.Register<IStatusBarColor>();

Just remove this sentence and it works nice.只需删除这句话,它就很好用。

Additionally, changing the registered class too StatusBarColor_Android, and not the base interface also works.此外,更改已注册的 class 也可以使用 StatusBarColor_Android,而不是基本接口也可以。 I found it just cleaner to remove the whole thing and let the [assembly...] do the job我发现移除整个东西并让[装配...]完成工作更清洁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM