Doubt with Dependencyservice Xamarin c#

Asked

Viewed 152 times

1

I created a project just android 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; }
    }
}

Setup :

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();
        }
    }
}

1 answer

0

Colon.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.