Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

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

$
0
0

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

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>