简体   繁体   中英

Ninject: Error activating string

Im following a guide of how to setup SQLite. In the guide he uses code-behind like this:

public MainPage()
{
     InitializeComponent();

     // Setup database
     var path = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "test.sqlite"));
     _connection = new SQLiteConnection(new SQLitePlatformWP8(), path); 
}

Im trying to do the same thing but instead by following MVVM. I thought his would be the way to go:

public override void Load()
{
    var path = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "test.sqlite"));

    Bind<ISQLitePlatform>().To<SQLitePlatformWP8>().WithConstructorArgument("test.sqlite", path);
}

Ninject responds with:

No matching bindings are available, and the type is not self-bindable.

Activation path:

  4) Injection of dependency string into parameter databasePath of constructor

  3) Injection of dependency SQLiteConnection into parameter connection of constructor

  2) Injection of dependency ICarRepository into parameter carRepository of constructor

  1) Request for MainVModel

Any tips on how to solve this one?

The SQLiteConnection needs the parameter and not the SQLitePlatformWP8 .

So change your registration to:

Bind<SQLiteConnection>.ToSelf().WithConstructorArgument("databasePath", path);
Bind<ISQLitePlatform>().To<SQLitePlatformWP8>();

Note: you need to use the correct paramter name "databasePath" which is definied in the SQLiteConnection class's constructor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM