Xamarin.Forms - FlashLight App
Tags: flashlight, torchlightapp, xamarin, xamarinforms
Published: 2019-11-13 17:21:34

---
title: Xamarin.Forms - FlashLight App
published: true
date: 2018-08-28 16:21:00 UTC
tags: Flash Light,Torch Light App,Xamarin,Xamarin.Forms
canonical_url: https://logeshpalani.blogspot.com/2018/08/xamarinforms-flashlight-app.html
---

 **Introduction**  
                     This article demonstrates how to create a FlashLight app in Xamarin.Forms application. This means if the user enables the toggle switch, the LED is turned on. Similarly, the user disables the toggle switch and the LED is turned off.  These features are not listed by default in a Xamarin.Forms application. So, we need to install a plugin for this.  
  

**NuGet Package - search Xamarin.Forms **

- [kphillpotts.Lamp.Plugin](https://github.com/kphillpotts/Xamarin.Plugins/ "Plugin Detail")
** Android Output**

  
  

| [![Xamarin](https://2.bp.blogspot.com/-dO02YggTyz4/W4V1mPfcg0I/AAAAAAAAK2U/sTFlgJfFQxgMahab2WGWw0iAmK24nCg-wCEwYBhgL/s320/Android%2BOutput.gif "Xamarin.Forms")](https://2.bp.blogspot.com/-dO02YggTyz4/W4V1mPfcg0I/AAAAAAAAK2U/sTFlgJfFQxgMahab2WGWw0iAmK24nCg-wCEwYBhgL/s1600/Android%2BOutput.gif) |
| Android Output |

  
  

Let's start.  
  

**Step 1**  
  
                     You can create a new Xamarin.Forms app by going to File \>\> New \>\> Visual C# \>\> Cross-Platform \>\> Cross-Platform App (Xamarin.Forms or Xamarin.Native), give the application name, and location directory path then click OK.

(Eg-Project Name: LampDemo)  

| [![Xamarin](https://3.bp.blogspot.com/-u41JsyR-tXU/W4V1mCgf0bI/AAAAAAAAK2Y/BgBHdsM5xO8DUD_y_YxABcuO9dy4UWaaQCEwYBhgL/s1600/Create.png "Xamarin.Forms")](https://3.bp.blogspot.com/-u41JsyR-tXU/W4V1mCgf0bI/AAAAAAAAK2Y/BgBHdsM5xO8DUD_y_YxABcuO9dy4UWaaQCEwYBhgL/s1600/Create.png) |
| Create |

  

  

**Step 2**  
  
                       After the project creation, add the following NuGet package to your project.

-  kphillpotts.Lamp.Plugin

| [![Xamarin](https://2.bp.blogspot.com/-gnIrorfpP18/W4V1mEhXH9I/AAAAAAAAK2c/OkAnE0cQbV4lpoKWbB6maquRn0DCiu4VQCEwYBhgL/s1600/Nuget.png "Xamarin.Forms")](https://2.bp.blogspot.com/-gnIrorfpP18/W4V1mEhXH9I/AAAAAAAAK2c/OkAnE0cQbV4lpoKWbB6maquRn0DCiu4VQCEwYBhgL/s1600/Nuget.png) |
| Nuget |

  

For that, open Solution Explorer and select your Solution. Right-click and select "Manage NuGet Packages for the Solution". In the popup window, click navigate to "Browse" tab and click browse "kphillpotts.Lamp.Plugin" and select following NuGet packages and select your project, then install it.

  

**Step 3**  
  
                       Now, open MainPage.xaml page. For that, go to Solution Explorer \>\> LampDemo (PCL) \>\>double click to open MainPage.xaml and here is the code of this page. In this page, we have designed one Toggle switch and this switch is used to turn on and turn off the LED.  
  

**XAML Code**

  

<script src="https://gist.github.com/logeshpalani98/78055d314d932f0c3a91dc7f59e671a2.js"></script>  
  

**Step 4**  
              Next, open Solution Explorer \>\> LampDemo(PCL) \>\> click open design view of MainPage.Xaml.cs and here is the code for this page.

- _CrossLamp.Current.TurnOn() - _To gain access to Turn On the LED.
- _CrossLamp.Current.TurnOff() - _To gain access to Turn Off the LED

**C# Code Behind**

<script src="https://gist.github.com/logeshpalani98/98e090c0b16cbfeface7a32af44d0823.js"></script>  
  

**Step 5**  
           Now, let us give the permission to access the FlashLight.
  
  

**Android Project **  
  
           Open AndroidManifest.xml file. For that, go to Solution Explorer \>\> LampDemo.Android \>\> double click to open AndroidManifest.xml and add the following permissions are enabled.  
  
<script src="https://gist.github.com/logeshpalani98/ff33267304508e2029a2f6768d4e0e9e.js"></script>  
  
**UWP Project **    
       
       Open Package.Appmanifest file. For that, go to Solution Explorer \>\>LampDemo.UWP \>\>click open Package.Appmanifest file and navigate to "Capabilities" tab then click the following permission checkbox here.  
  
<script src="https://gist.github.com/logeshpalani98/c22ede1e1c19c261a845ed3a1df53f88.js"></script>  
  

**Step 6**  
  
           Now, go to "Build " menu and click "Configure  Manager".Here configure your startup projects. Click F5 or start to build and run your application.

The end result like below,

  

**Android Output**

  

**         Finally, we have successfully created a Xamarin.Forms LampDemo application using kphillpotts.Lamp.Plugin.**  
  

* * *
**Full Source Code can be found [Here](https://github.com/logeshpalani98/FlashLight-App "Source Code").**