Working With Files in Xamarin.Forms

Xamarin.Forms
img_xamarin

Xamarin.Forms code runs on multiple platforms – each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

  • Create a new solution and name it as “ Workingwithfiles.cs”
  • Add the following two files into the project
    using System.IO;
    using System.Reflection;
  • Now we have to add a text file to our project, for adding a  new file just right click on the  solution pane and  select the  Add-> New File
    xf14.1
  • Name the newly created file as “PCLTextResourceFile.txt” as below
    xf14.2
  • We have to create a label to load the  contents of the file in it, and set its height.
  • Remember that if we want to use the text file than we have to make it as a “embedded resource”
    xf14.3
  • Now we can use the text file, so first step is to load the texfile in to the current context add the following code for that. “”WorkingwithFiles.PCLTextResource.txt”” is the path of the file .
  • To load the file we have created a stream type object.
  • Now we havae to read the contents from the stream and convert it to the text format , add the following code to do it
  • Now We Take a Lable so We Display A Heading Text, Following Code For That
  • Now Our App is Ready To Run, Lets Run The App.
    xf14.4

If you like this tutorial then you can download full copy of the code from github

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">