In today’s article, I will discuss how to use the File System of Xamarin forms in data storage. File System storage is a great option for you if you want to store a more larger data in the file system, maybe in the form of XML or JSON file, if Preference storage is not enough for you and Database is an overkill for you.
I will be using Xamarin Forms PCL type project and will be targeting both the Android and UWP platforms. Our sample application will be basic Read/Write operations from a Json file as shown in Figure 1 and Figure 2.
Like the strategy we used in Preference, keep in mind that the implementation of Reading/Writing in file storage is different per platform. Android and IOS may use standard .NET classes like File Operations or in my case, I used Text Writer and Reader and UWP uses only Windows Storage.
Let’s create a blank Xamarin Forms PCL type project and modify our MainPage.xaml file so that it will look like Figure 3. Then let’s also add some event handlers in its code behind.
Next, we design our Interface to handle both the operations of File APIs for both Android and UWP. Instead of using the traditional File Operations, we will use the TextWriter and TextReader due to asynchronous operations that are required to be implemented by the Windows Storage in the UWP. Kindly see the interface definition in Figure 4.
We then add per platform the Implementations of our IFileReadWrite Interface:
Important notes on the Android Implementation:
- IFileReadWrite implementation should be registered with the Dependency serviceas a metadata attribute above its namespace.
- Android and IOS implementation may use the same TextWriter and TextReader classes in order to Read/Write to a file. The only difference is the path of the File to be processed.
Important note on UWP Implementation:
- Unlike the Android and iOS platforms, the implementation of IPreference in the App.xaml.cs should be registered in UWP as shown in Figure 7.
- UWP Implementation uses the Windows Storage class.
We need to create a wrapper class called FileService for wrapping the Read/Write functionalities per platform and also add some Json File Serialization/Deserialization using the Newtonsoft Json plugin. Kindly see figure 8.
Let’s create a static property in our MainPage.xaml.cs file which will be used to access our wrapper class FileService as shown in Figure 9.
We will start to code in our MainPage.xaml.cs for our Add and Refresh button events as shown in figure 10.
Try to build and run our Android and UWP application. We expect that the output should be the same with Figure 1 and 2.
Just a quick wrap up for the things we’ve learned, if you require more storage or the business requirement needs to Read/Write in files then your preferred storage technique should be File System. Also remember that different platform implements different File APIs so we need to have an abstraction using the built in Xamarin Forms dependency service. In my next article, I will discuss the third strategy on data storage which is Database.
Happy Coding 🙂