Introduction
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the data files with an app.
User-writable-storage can be implemented natively and then accessed using the DependencyService.
DependencyService
DependencyService allows apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do.
DependencyService is a dependency resolver. In practice, an interface is defined and DependencyService finds the correct implementation of that interface from the various platform projects.
Xamarin.Forms apps need three components to use DependencyService
- Interface – The required functionality is defined by an interface in shared code.
- Implementation Per Platform – Classes that implement the interface must be added to each platform project.
- Registration – Each implementing class must be registered with DependencyService via a metadata attribute. Registration enables DependencyService to find the implementing class and supply it in place of the interface at runtime.
- Call to DependencyService – Shared code needs to explicitly call DependencyService to ask for implementations of the interface.
Following Topic Covered.
- Create Directory
- Rename Directory
- Delete Directory
Related Post
Prerequisites
- Visual Studio 2017(Windows or Mac)
The steps given below are required to be followed in order to create a Directory, rename it, and delete using DependencyService in Xamarin.Forms, using Visual Studio.
Setting up a Xamarin.Forms Project
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
Choose the Xamarin.Forms App project under Cross-platform/App in the New Project dialog.
Name your app, select “Use Portable Class Library” for shared code, and target both - Android and iOS.
You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click "Create".
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
Creating Interface
Create - interface in Xamarin.Forms PCL.
Go to Solution—>PCL—>Right click—>New—>Empty Class—>IDirectory.cs.
Now, write the following code.
IDirectory.cs
- using System;
- namespace XamarinForms_Files
- {
- public interface IDirectory {
- string CreateDirectory(string directoryName);
- void RemoveDirectory();
- string RenameDirectory(string oldDirectoryName, string newDirectoryName);
- }
- }
Implementation per platform - iOS Implementation
Go to Solution—>iOS—>Right click—>New—>Empty Class—> DirectoryHelper.cs.
Now, write the following code.
DirectoryHelper.cs
- using System;
- using System.IO;
- using Xamarin.Forms;
- using XamarinForms_Files.iOS;
- [assembly: Dependency(typeof(DirectoryHelper))]
- namespace XamarinForms_Files.iOS {
- public class DirectoryHelper: IDirectory {
- public string documentBasePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- public string CreateDirectory(string directoryName) {
- var directoryPath = Path.Combine(documentBasePath, directoryName);
- if (!Directory.Exists(directoryPath)) {
- Directory.CreateDirectory(directoryPath);
- }
- return directoryPath;
- }
- public void RemoveDirectory() {
- DirectoryInfo directory = new DirectoryInfo(documentBasePath);
- foreach(DirectoryInfo dir in directory.GetDirectories()) {
- dir.Delete(true);
- }
- }
- public string RenameDirectory(string oldDirectoryName, string newDirectoryName) {
- var olddirectoryPath = Path.Combine(documentBasePath, oldDirectoryName);
- var newdirectoryPath = Path.Combine(documentBasePath, newDirectoryName);
- if (Directory.Exists(olddirectoryPath)) {
- Directory.Move(olddirectoryPath, newdirectoryPath);
- }
- return newdirectoryPath;
- }
- }
- }
Android Implementation
Go to Solution—>Droid—>Right click—>New—>Empty Class—> DirectoryHelper.cs.
Now, write the following code.
DirectoryHelper.cs
- using System;
- using System.IO;
- using Xamarin.Forms;
- using XamarinForms_Files.Droid;
- [assembly: Dependency(typeof(DirectoryHelper))]
- namespace XamarinForms_Files.Droid {
- public class DirectoryHelper: IDirectory {
- public string documentBasePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- public string CreateDirectory(string directoryName) {
- var directoryPath = Path.Combine(documentBasePath, directoryName);
- if (!Directory.Exists(directoryPath)) {
- Directory.CreateDirectory(directoryPath);
- }
- return directoryPath;
- }
- public void RemoveDirectory() {
- DirectoryInfo directory = new DirectoryInfo(documentBasePath);
- foreach(DirectoryInfo dir in directory.GetDirectories()) {
- dir.Delete(true);
- }
- }
- public string RenameDirectory(string oldDirectoryName, string newDirectoryName) {
- var olddirectoryPath = Path.Combine(documentBasePath, oldDirectoryName);
- var newdirectoryPath = Path.Combine(documentBasePath, newDirectoryName);
- if (!Directory.Exists(olddirectoryPath)) {
- Directory.Move(olddirectoryPath, newdirectoryPath);
- }
- return newdirectoryPath;
- }
- }
- }
Setting up the User Interface.
Go to MainPage.Xaml and write the following code.
MainPage.Xaml
- <?xml version="1.0" encoding="utf-8"?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinForms_Files" x:Class="XamarinForms_Files.XamarinForms_FilesPage">
- <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="Center">
- <Entry x:Name="txtDirectory" Placeholder="Enter Directory Name..."></Entry>
- <Button x:Name="btnCreate" Text="Create"></Button>
- <Entry x:Name="txtDirectoryRename" Placeholder="Enter New Directory Name..."></Entry>
- <Button x:Name="btnRename" Text="Rename"></Button>
- <Button x:Name="btnRemove" Text="Remove"></Button> </StackLayout>
- </ContentPage>
Call to DependencyService
In this step, call the DependencyService for your PCL.
- using Xamarin.Forms;
- namespace XamarinForms_Files {
- public partial class XamarinForms_FilesPage: ContentPage {
- string oldDirectoryName;
- public XamarinForms_FilesPage() {
- InitializeComponent();
- btnCreate.Clicked += (sender, e) => {
- oldDirectoryName = txtDirectory.Text;
-
- DependencyService.Get < IDirectory > ().CreateDirectory(txtDirectory.Text);
- };
- btnRename.Clicked += (sender, e) => {
-
- DependencyService.Get < IDirectory > ().RenameDirectory(oldDirectoryName, txtDirectoryRename.Text);
- };
- btnRemove.Clicked += (sender, e) => {
-
- DependencyService.Get < IDirectory > ().RemoveDirectory();
- };
- }
- }
- }
Click the Play button to try it out.
Here is the output.
I hope you have understood how to create a Directory, rename it, and delete a file using DependencyService.
Thanks for reading. Please share comments and feedback.