How To Create File Picker In Xamarin.Forms

Introduction
 
This article demonstrates how to create a file picker in a Xamarin.Forms Application. 
 
Let's start.
 
Step 1 
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross Platform app, then give your project a name and location.
 
 
 
Step 2
 
Install the following NuGet Packages to your project.
  • Xam.Plugin.FilePicker
    Now Update the following NuGet Packages to your project.
  • Xamarin.Forms
    Go to Solution Explorer and Select your solution. Right click and select "Manage NuGet Packages for a solution".
 
 
 Now, select the following NuGet packages and select your project to install the packages.
 
 
 
 
 
Step 3 
 
Open Solution Explorer >> Project Name >> MainPage.xaml. Open the Design View of this page.
 
 
 
The code is given below.
 
 
 
XAML Code 
 
We are Creating Button and Label Inside StackLayout.
  1. <StackLayout>  
  2.        <Button Text="Pick File" Clicked="Button_Clicked" HorizontalOptions="Center"  
  3.                VerticalOptions="Center"/>  
  4.        <Label Text="File Is :" x:Name="lbl"/>  
  5.    </StackLayout>  
Step 4 
 
Open Solution Explorer >> Project Name >> MainPage.Xaml.cs. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
C# Code  
  1. using Plugin.FilePicker;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace FilePicker  
  10. {  
  11.     public partial class MainPage : ContentPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private async void Button_Clicked(object sender, EventArgs e)  
  19.         {  
  20.             var file = await CrossFilePicker.Current.PickFile();  
  21.   
  22.             if (file != null)  
  23.             {  
  24.                 lbl.Text = file.FileName;  
  25.             }  
  26.         }  
  27.     }  
  28. }  
Step 5 
 
Next, go to Project Name.Android >> Properties.
 
 
 
Next, double click to the left >> Open the Design Page >> click on Android Manifest.
 
 
For required permissions select on "READ EXTERNAL STORAGE" and "WRITE EXTERNAL STORAGE".


Step 6 
 
Next, select the build & deploy option, followed by selecting from the list of Android Emulators or Simulator. You can choose any API (Application Program Interface) Level Emulator and Simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.  
 
Click on  Pick a File and then Choose Filename will be displayed.

 
Finally, we have successfully created Xamrin.Forms File Picker.
G
M
T
 
Text-to-speech function is limited to 200 characters
G
M
T
 
Text-to-speech function is limited to 200 characters


Similar Articles