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.
- <StackLayout>
- <Button Text="Pick File" Clicked="Button_Clicked" HorizontalOptions="Center"
- VerticalOptions="Center"/>
- <Label Text="File Is :" x:Name="lbl"/>
- </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
- using Plugin.FilePicker;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
-
- namespace FilePicker
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- private async void Button_Clicked(object sender, EventArgs e)
- {
- var file = await CrossFilePicker.Current.PickFile();
-
- if (file != null)
- {
- lbl.Text = file.FileName;
- }
- }
- }
- }
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.
| | | | | | | | | |
Text-to-speech function is limited to 200 characters
| | | | | | | | | |
Text-to-speech function is limited to 200 characters