Xamarin - Dependencyservice NULL

Asked

Viewed 236 times

2

Dear friends, good morning!

I started seeing a tutorial to develop a simple app in Xamarin with Sqlite, but I have a problem, with an object that calls Dependencyservice, they always come null, and I’m not managing to find the solution.

Below is the source code:

Connection class:

inserir a descrição da imagem aqui

Iconfig interface:

inserir a descrição da imagem aqui

Problem:

inserir a descrição da imagem aqui

My config object, is null, how do I adjust it? I read to add an attribute of [Assembly: Depencendy(typeof(xxxxx)]. I don’t understand about it, I don’t know what I need to go through in the typeof, if I pass my current class, or if I pass the Dependencyservice class.

I await answers.

Att.

Felipe Duarte

  • 1

    [assembly: Dependency(typeof(ConfigDb))] Is an annotation that should be in the class (public class ConfigDb : IConfigDb) specific to each platform. Your project is of the type xamarin forms?

1 answer

4

If you are using Xamarin Forms, you probably need to implement the Iconfig interface on each platform. For example, in the Meudb.Droid project, add the following class:

[assembly: Dependency(typeof(ConfigDroid))]
namespace MeuDb.Droid
{
    public class ConfigDroid : IConfig
    {

        private string DiretorioSQLite
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            }
        }

        private ISQLitePlatform Plataforma
        {
            get
            {
                return new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            }
        }
    }
}

Then you need to do the same implementation for other platforms you’re working on, IOS or Windows Phone.

Browser other questions tagged

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