Doubt with DependencyService xamarin C #

1

I made the creation of an android project only using xamarin Problem: Problem at this point does not recognize DependencyService

An interface:

namespace Projeto.Xamarin.Android
{
    public interface IConfig
    {
        string DiretorioDB { get; }
        ISQLitePlatform Plataforma { get; }
    }
}

Configuration:

namespace Projeto.Xamarin.Android
{ 
   public class Config : IConfig
    {

        private string diretorioDB;
        private ISQLitePlatform plataforma;


        public string DiretorioDB
        {
            get
            {
                if (string.IsNullOrEmpty(diretorioDB))
                {
                    diretorioDB = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                }
                return diretorioDB;
            }
        }

        public ISQLitePlatform Plataforma
        {
            get
            {
                if (plataforma == null)
                {
                    plataforma = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
                }
                return plataforma;
            }
        }
    }
}

DataAcess:

namespace XFEmpleados
{
    class DataAccess : IDisposable
    {
      private SQLiteConnection connection;

        public DataAccess()
        {
            //problema neste ponto não reconhece DependencyService
            //   var config = DependencyService.Get<IConfig>();
            //   connection = new SQLiteConnection(config.) 
        }



        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
    
asked by anonymous 05.07.2016 / 22:18

1 answer

0

Two points.

  • The IConfig interface should be in the main project (Portable), not in the Android Project
  • You have to put a note in the Config class, so it will look like this:
[assembly: Dependency(typeof(Config))]
namespace Projeto.Xamarin.Android
{
    public class Config : IConfig
    {
        ...
    }
}
    
14.10.2016 / 20:48