javirs
javirs

Reputation: 1100

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. Anyhow I still get the System.MissingMethodException: 'Default constructor not found for type Foodies.VisualEffects.IStatusBarColor'message.

I declare my base interface in the common project at Foodies/Views/VisualEffects/iStatusBarColor.cs, like this:

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

Then in my android project I add StatusBarColor_Android, looking like:

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

This is my android project settings android project settings

Im new to Xamarin, could someone help me finding the error ??

Upvotes: 1

Views: 1523

Answers (2)

Leo Zhu
Leo Zhu

Reputation: 14956

The namespace in the code above is confusing,as you said

I declare my base interface in the common project at Foodies/Views/iStatusBarColor.cs

Normally if you don't customize namepace, it should be:

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

and in you Android project,why you have two different reference using Foodies.Droid; and using Foodies.VisualEffects.Droid;,and you didn't reference the namepace Foodies.Views when you implement 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))]

2.call 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).

Upvotes: 0

javirs
javirs

Reputation: 1100

Found out in addition to all of this, in the MainActivity, in the OnCreate I had

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. I found it just cleaner to remove the whole thing and let the [assembly...] do the job

Upvotes: 1

Related Questions