Page 46 - MSDN Magazine, June 2017
P. 46
Notice how the LoginAsync method invokes the platform- specific implementation of the IAuthentication interface via the DependencyService.Get method, passing the active instance of the MobileServiceClient class and the current authentication provider.
The UI
The UI for the sample application is very simple. If a ListView is data-bound to an instance of the ViewModel, an Activity- Indicator will show the progress indicator when the app is busy, and a few buttons are data-bound to commands in the View- Model. The XAML for the UI is shown in Figure 12 and goes in the MainPage.xaml file.
In the codebehind, you must assign an instance of the ViewModel to the binding context of the page and you’ll need to check that login is necessary every time the user opens the page. Here’s the code:
private BookViewModel ViewModel { get; set; } public MainPage()
{
InitializeComponent(); this.ViewModel = new BookViewModel(); this.BindingContext = this.ViewModel;
}
protected async override void OnAppearing() {
base.OnAppearing();
if (this.ViewModel.IsUserAuthenticated == false) await this.ViewModel.LoginAsync(); }
LoginAsync is called within the OnAppearing method because this lets you call asynchronous methods at page initialization and every time a page is displayed.
Configuring the App for Analytics
and Crash Reporting
With a single line of code, you can configure your Xamarin.Forms app to send usage and crash reports to Visual Studio Mobile Center.
Figure 13 The Sample App Running on Android 42 msdn magazine
In the App.xaml.cs, override the OnStart method as follows:
// Requires using directives for Microsoft.Azure.Mobile,
// Microsoft.Azure.Mobile.Analytics and Microsoft.Azure.Mobile.Crashes protected override void OnStart()
{
MobileCenter.Start("android={Your Android App secret here};" + "ios={Your iOS App secret here}",
typeof(Analytics), typeof(Crashes));
}
As you can see, you can enable both the Android and iOS projects by specifying the app secret, which can be found in the Getting Started page of Mobile Center. All the reports can be found in the Analytics and Crashes pages of Mobile Center.
Testing the Application
If you now run the sample app, the first thing you’ll see is the login screen for the Microsoft account. Once you’ve been authenticated, you’ll be able to view, edit and save the list of books, as demon- strated in Figure 13.
Of course, a similar result will appear on iOS devices. If you try to add and save some items, and then go to the table page in Visual Studio Mobile Center, you’ll see a grid showing your records. Further improvements to the code might involve implementing offline sync with the Azure Mobile Client SDK and more specific error handling in case the user isn’t authenticated.
Further improvements to the code might involve implementing offline sync with the Azure Mobile Client SDK and more specific error handling in case the user isn’t authenticated.
Wrapping Up
Visual Studio Mobile Center dramatically simplifies the way you implement back-end services for your mobile apps—such as authen- tication and tables—not only with a convenient UI, but also by setting up most of the backing infrastructure on the Azure Mobile Apps service on your behalf. Visit the Mobile Center frequently in order to verify the availability of new features. n
AlessAndro del sole has been a Microsoft MVP since 2008. Awarded MVP of the Year five times, he has authored many books, eBooks, instructional videos and articles about .NET development with Visual Studio. Del Sole works as a senior .NET developer, focusing on .NET and mobile app development, training, and consulting. Follow him on Twitter: @progalex.
ThAnks to the following technical expert for reviewing this article: Adrian Hall Xamarin