How to use the DepencyService correctly to save files...

SzbestaSzbesta Member ✭✭✭

Hey everyone!
I'm quite sure that I'm just having a little issue with using the DependencyServicce that drives me nuts... I have read through the documentation on the Mircrosoft Website and Implented a SaveAndLoad feature. Now everytime i call the DependencyService to save a File i get a NullReference Exception. I have read the other posts which were suggesting to register the concrete plattform specific class to the DependencyService, but i did that so i dont know where i went wrong.
Interface in the PCL

 public interface ISaveAndLoad
    {
        void SaveText(string filename, string text);
        string LoadText(string filename);
    }

Concrete implementation in the Android Project

[assembly: Dependency(typeof(Test.Droid.SaveAndLoad))]
namespace Test.Droid
{
    public class SaveAndLoad
    {
        public void SaveText(string filename, string text)
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var filePath = Path.Combine(documentsPath, filename);
            File.WriteAllText(filePath, text);
        }
        public string LoadText(string filename)
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var filePath = Path.Combine(documentsPath, filename);
            return System.IO.File.ReadAllText(filePath);
        }
    }
}

Call to the DepndencyService in the DAL

DependencyService.Get<ISaveAndLoad>().SaveText("tasks.json", json);

Best Answer

  • SzbestaSzbesta ✭✭✭
    Accepted Answer

    Ok NVM I'm a bit of a dummy. Just had to change the class declaration of the concrete class to

    public class SaveAndLoad:ISaveAndLoad
    

Answers

  • SzbestaSzbesta Member ✭✭✭
    Accepted Answer

    Ok NVM I'm a bit of a dummy. Just had to change the class declaration of the concrete class to

    public class SaveAndLoad:ISaveAndLoad
    
Sign In or Register to comment.