DependencyService.Get<ILocalize>() returns null

This line of code will always return a NullReferenceException:

I'm most likely missing a point with the DependencyService, but I can't find out what I'm doing wrong. Any help is highly appreciated!

This is how I set the DependencyService:

protected override void OnInitialized()
{
InitializeComponent();
Toolkit.Toolkit.Init();
CommonUtils.DependencyService = DependencyService.Get();
NavigationService.NavigateAsync("MainPage");
}

Then I'm calling this line of code and it will return a NullReferenceException:

return DependencyService.Get<ILocalize>().GetCurrentCultureInfo();

System.NullReferenceException: Object reference not set to an instance of an object error

The DependencyService is not null, but DependencyService.Get<ILocalize>() will always return null.

Interface ILocalize:

using System.Globalization;

namespace Toolkit
{
    public interface ILocalize
    {
        CultureInfo GetCurrentCultureInfo();
    }
}

The class Localize in the Toolkit.Droid project:

using System.Globalization;
using Xamarin.Forms;

[assembly: Dependency(typeof(Toolkit.Droid.Localize))]

namespace Toolkit.Droid
{
    public class Localize : ILocalize
    {
        public CultureInfo GetCurrentCultureInfo()
        {
            var androidLocale = Java.Util.Locale.Default;
            var netLanguage = androidLocale.ToString().Replace("_", "-"); // turns pt_BR into pt-BR
            return new CultureInfo(netLanguage);
        }
    }
}

Thanks for any help!

Answers

  • kenneth.leekenneth.lee USMember ✭✭✭

    Can you send a stack trace?

  • DerProgrammiererDerProgrammierer DEMember ✭✭✭
    edited February 2017

    @kenneth.lee said:
    Can you send a stack trace?

    Sure, here you are:

    " at Toolkit.CommonUtils.GetCurrentCultureInfo () [0x00002] in C:\proj\Toolkit\Toolkit\Utils\CommonUtils.cs:18 "

    http://i.imgur.com/NYfMMyK.png

    EDIT:

    This is where my CommonUtils class where GetCurrentCultureInfo is called:

    namespace Toolkit
    {
        public class CommonUtils
        {
            public static IDependencyService DependencyService
            {
                get;
                set;
            }
    
            public static CultureInfo GetCurrentCultureInfo()
            {
                try
                {
                    return DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
    }
    
  • DerProgrammiererDerProgrammierer DEMember ✭✭✭

    At the end of the DependencyService Instruction, I found this note:

    Note: You must provide an implementation in every platform project. If no Interface implementation is registered, then the DependencyService will be unable to resolve the Get() method at runtime.

    I think this might be the problem. I didn't start implementing iOS yet, so I didn't create the project Toolkit.iOS so far. This is my Solution Structure:

    Do I have to add a Toolkit.iOS? I would like to avoid that until I really need it.

  • MonteHansenMonteHansen USUniversity ✭✭

    I just experienced the same problem, where the iOS platform wouldn't .Get<>. As soon as I registered it on the Android platform -- bada bing.

Sign In or Register to comment.