Introduction
Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
Browse Code here.
Prerequisites
The steps given below are required to be followed in order to read a text file in Xamarin iOS, using Xamarin Studio.
Step 1
Go To Xamarin Studio.
Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.
Step 2
In this step, configure your app. Give the app a name (Ex:sample), Organization Identifier, and afterwards, click Next.
step 3
In this step, give your project a name (Ex: Sample) and solution name (Ex: Sample). Give path of your project and click Create.
Step 4
Subsequently, go to the solution. In there, get all the files and sources in your project. Now, select Main.storyboard and double click to open Main.storyboard page.
Step 5
After opening the Main.storyboard, you can design this page, as per your desire.
Step 6
Now, go to the Toolbox window and scroll down. You will see all the tools and controls. Just drag and drop the Button.
Next, you need to align the Button, using constraints.
Step 7
Now, go to the Properties window. Select the Button and give it a name (Ex:btnRead).
Step 8
You need to drag and drop the label. Next, you need to align the Label, using constraints.
Step 9
Now, go to the Properties window and select another Label (Ex:lblDisplay).
Step 10
In this step, go to the ViewController.cs page and add the namespace as given below.
ViewController.cs
- using System.IO;
- using System.Threading;
Step 11
Now, go to the ViewController.cs page and write the follwoing code.
ViewController.cs
- namespace XamariniOSReadFile {
- public partial class ViewController: UIViewController {
- protected ViewController(IntPtr handle): base(handle) {
-
- }
- public override void ViewDidLoad() {
- base.ViewDidLoad();
-
- }
- private int ReadFile() {
- int count = 0;
- using(StreamReader reader = new StreamReader("/Users/DELPIN/Desktop/xamarin.txt")) {
- string filecontent = reader.ReadToEnd();
- count = filecontent.Length;
- Thread.Sleep(5000);
- }
- return count;
- }
- partial void BtnRead_TouchUpInside(UIButton sender) {
- int count = ReadFile();
- lblDisplay.Text = count.ToString();
- }
- public override void DidReceiveMemoryWarning() {
- base.DidReceiveMemoryWarning();
-
- }
- }
- }
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) : e[f];
- }
Step 12
Now, go to Run option and choose "Debug". From the list of iPhone and iPad simulators, you can choose any one simulator and run it.
Output
After a few seconds, the app will start running on your iPhone simulator.You will see your app working successfully.
You can click "Read File" button that will read how many words are there in your file.
That's it. I hope you liked this article.