_________________
As a developer, you're likely no stranger to the world of cross-platform app development. But what if you could open web links in your users' default browsers without having to worry about platform-specific APIs? Enter Xamarin Essentials, a powerful tool that allows you to access shared code across Android, iOS, and UWP platforms.
In this article, we'll explore how to use Xamarin Essentials to open web links with the default browser in a Xamarin Forms application. With this technique, you can leverage the power of C# to create cross-platform apps that work seamlessly across multiple devices and platforms.
Getting Started
Before diving into the code, let's cover the essential tools you'll need for developing your Xamarin Forms app:
- Windows 10 (recommended)
- Visual Studio 2017
- Android API 19 or higher and UWP 10.0.16299.0 or higher
Now that we have our tools in place, let's walk through the step-by-step process of creating a new project.
Step 1: Create a New Project
Open Visual Studio 2017 and select "New Project" under Visual C# -> Mobile App (Xamarin.Forms). Give your app a suitable name, such as "XamFormBrowser," and click OK.
Step 2: Select the Cross-Platform Template
Select the Cross-Platform template as a Blank APP. Set the platform to Android and UWP, and choose .NET standard for code sharing strategy. Visual Studio will create three projects: Portable, Android, and UWP.
Step 3: Add Xamarin Essentials Reference
Right-click your solution (XamFormBrowser) and select "Manage NuGet Packages." Browse and search for Xamarin.Essentials, selecting the package and installing it across all three projects (Portable, Android, and UWP).
Step 4: Design Your UI
In Mainpage.Xaml, add a Label, Entry, and Button controls to display title and weblink, as well as a browse button:
`xml
`
Step 5: Add Code in MainActivity.cs
In XamFormBrowser.Android project, add the following code in the onCreate() method:
`csharp
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
`
Also, add the below method:
`csharp
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
`
Step 6: Add Namespace and Code in Mainpage.Xaml.cs
Add the following namespace and code:
`csharp
using Xamarin.Essentials;
protected override void OnAppearing() {
base.OnAppearing();
}
async Task btnBrowse_Click(object sender, System.EventArgs e) {
Uri = new Uri(entURL.Text);
await Browser.OpenAsync(Uri, BrowserLaunchMode.SystemPreferred);
}
`
Step 7: Deploy Your App
Deploy your app in UWP and Android. The output of the XamFormBrowser App is:
With these steps, you've successfully opened a web link using default browser in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.
Now that you know how to unlock swift app development with Xamarin Essentials, what will you build next? The possibilities are endless!