Xamarin.Forms - Crashes In Visual Studio App Center

Introduction
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
In this article, you will learn how to collect crash reports using Visual Studio App Center in Xamarin.Forms. Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that the process of reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
Visual Studio App Center
Xamarin.Forms - Crashes in Visual Studio App Center
 
App Center has multiple services that are most commonly used by mobile developers, where multiple services act as a single integrated product. With the use of a single integrated product, you can build, test, distribute, and monitor your mobile apps, and also implement push notifications.
 
Support Platforms
  1. Android
  2. iOS
  3. React Native
  4. UWP
  5. Xamarin
  6. macOS
  7. Cordova
Xamarin.Forms - Crashes in Visual Studio App Center
 
App Center Services
  • Build
  • Diagnostics (Formerly Crashes)
  • Test
  • Analytics
  • Distribute
  • Push Notifications
Xamarin.Forms - Crashes in Visual Studio App Center
 
Crashes
 
Crashes happen when a runtime exception occurs from an unexpected event. Usually, a crash terminates the app. These are the errors not handled by a try/catch block. When a crash occurs, App Center will record the details of the crash, then the state of the app and device automatically generate a crash log based on the records. These logs contain valuable information about the crash which helps us fix the crash.
 
Prerequisites
  • Visual Studio 2017(Windows or Mac)
  • Visual Studio App Center Account
Setting up a Xamarin.Forms Project
 
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself. Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
Name your app, select “Use Shared Library” for shared code, and target both - Android and iOS.
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
You now have a basic Xamarin.Forms app. Click the "Play" button to try it out.
Xamarin.Forms - Crashes in Visual Studio App Center 
 
Create an app in the App Center (iOS)
 
In this step, create an app in the App Center by going through the following link.
 
Now, sign in using your preferred account.
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
Add New app
 
In this step, give your app Name (Ex: MyApp) and description.
 
 OS iOS
 Platform Xamarin
 
Afterward, click Add New app.
 
Xamarin.Forms - Crashes in Visual Studio App Center
 
Now, your App Center app is ready. You can use it now.
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
Setting up the User Interface
 
Go to MainPage.Xaml and write the following code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinAppCenter" x:Class="XamarinAppCenter.MainPage">  
  3.     <StackLayout>    
  4.         <StackLayout HorizontalOptions="Center" VerticalOptions="Start">    
  5.          <Image Margin="0,50,0,0" x:Name="imgBanner" Source="banner.png" ></Image>    
  6.          <Image Margin="0,0,0,10" x:Name="imgAppCenter" HeightRequest="200" Source="Appcenter.jpg" ></Image>    
  7.          <Label Margin="0,0,0,10" Text="App Center Crash Report" FontAttributes="Bold" FontSize="Large" TextColor="#CA6F1E" HorizontalTextAlignment="Center" ></Label>    
  8.          <Button x:Name="btnCrash" Text="Crash Me" Clicked="btnCrash_Clicked" />    
  9.     
  10.         </StackLayout>    
  11.     </StackLayout>    
  12. </ContentPage>  
Click the Play button to try it out.
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
Add AppCenter Crashes NuGet
 
In this step, add AppCenter Crashes to your project. You can install Microsoft.AppCenter.Crashes via NuGet, or you can browse the source code on GitHub.
 
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Microsoft.AppCenter.Crashes" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP).
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
AppCenter.Crashes requires platform-specific setup

Xamarin.forms PCL
 
The following steps are necessary for PCL.
 
In the PCL project's App.xaml.cs that is launched AppCenter.Crashes must be initialized in the OnStart() method.
 
App.xaml.cs
  1. using Microsoft.AppCenter;  
  2. using Microsoft.AppCenter.Crashes;  
  3.   
  4. [assembly: XamlCompilation(XamlCompilationOptions.Compile)]  
  5. namespace XamarinAppCenter  
  6. {  
  7.     public partial class App : Application  
  8.     {  
  9.         public App()  
  10.         {  
  11.             InitializeComponent();  
  12.   
  13.             MainPage = new MainPage();  
  14.         }  
  15.   
  16.         protected override void OnStart()  
  17.         {  
  18.             // Handle when your app starts  
  19.             AppCenter.Start("ios=d0bde07f-****-4e43-****-1fb50b1023fb;" +  
  20.                   "uwp={Your UWP App secret here};" +  
  21.                   "android={Your Android App secret here}",  
  22.                             typeof(Crashes));  
  23.         }  
  24.   
  25.         protected override void OnSleep()  
  26.         {  
  27.             // Handle when your app sleeps  
  28.         }  
  29.   
  30.         protected override void OnResume()  
  31.         {  
  32.             // Handle when your app resumes  
  33.         }  
  34.     }  
  35. }  
iOS
 
The following steps are necessary for iOS.
 
In the iOS project's AppDelegate that is launched AppCenter.Crashes must be initialized in the FinishedLaunching() method.
 
AppDelegate.cs
  1. public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
  2.         {  
  3.   
  4.             global::Xamarin.Forms.Forms.Init();  
  5.             LoadApplication(new App());  
  6.   
  7.             AppCenter.Start("d0bde07f-****-4e43-****-1fb50b1023fb",  typeof(Crashes));  
  8.   
  9.             return base.FinishedLaunching(app, options);  
  10.         }  
Android
 
You need to create another app in the App Center with OS option as Android.
https://appcenter.ms/
 
UWP
 
You need to create another app in the App Center with OS option as Windows.
https://appcenter.ms/
 
Crash
 
In this step, write the following code for a simple crash.
 
MainPage.xaml.cs
  1. using Xamarin.Forms;  
  2.   
  3. namespace XamarinAppCenter  
  4. {  
  5.     public partial class MainPage : ContentPage  
  6.     {  
  7.         public MainPage()  
  8.         {  
  9.             InitializeComponent();  
  10.         }  
  11.   
  12.         void btnCrash_Clicked(object sender, System.EventArgs e)  
  13.         {  
  14.             throw new NotImplementedException();  
  15.         }  
  16.     }  
  17. }  
Click the Play button to try it out.
 
Xamarin.Forms - Crashes in Visual Studio App Center
 
Crash Details
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
Crash Group Details
 
Xamarin.Forms - Crashes in Visual Studio App Center 
 
I hope you have understood how to collect Crash Reports using Visual Studio App Center in Xamarin.Forms.
 
Thanks for reading. Please share comments and feedback.


Similar Articles