In this blog post I’ll explain how to create a simple Twitter client with Xamarin Forms and .NET Standard 2.0. You can download the source code of the app from my GitHub repository here. xwitterScreenshot

Known issues The UWP flavor of the app crashes at the moment of this writing because of this known bug of Xamarin.Auth. The iOS version of the app is not tested. If you encouter the followin error or random deployment errors you need to delete bin and obj folder of the project and redeploy. Running Visual Studio 2017 as admin may help.

Could not locate c:\temp\IC6.Xwitter\IC6.Xwitter\packages.config.  Ensure that this project has Microsoft.Bcl.Build installed and packages.config is located next to the project file. 2 IC6.Xwitter.Android

21.pngCreate a new solution

We start with a brand new Visual Studio Solution to create a Xamarin.Forms app from the File-> New Project menu.

  1. Cross-platform
  2. Cross-platform app (Xamarin Forms)
  3. We choose a name
  4. Hit OK.

1 We set blank app, all the platforms, Xamarin.Forms as UI Technology and .NET Standard as Code Sharing Strategy.2

Reorganize some files

We now have a blank app ready to be developed. We reorganize some files in the shared library to reflect the MVVM architecture of our app. We create the Models, ViewModels and Views folders. 3 We move the MainPage.xaml with drag and drop into the Views folder because the MainPage is a view too. 5 Now we have to move the MainPage class in the correct namespace: IC6.Xwitter.Views. 6.png The last step for this section is to provide the right namespace in the bootstrap of the app as well. 7

Add NuGet Packages

Xamarin.Auth

To make calls to the Twitter APIs we need to perform OAuth authentication. We can add the Xamarin.Auth NuGet package to avoid the implementation of OAuth. The Xamarin.Auth library provides a specific implementation for every platform because, for example, it needs to open a browser to perform the steps required by the OAuth standard. Because of that we need to add this package to every project in our solution. We right-click the solution in the Solution Explorer and then click on Manage NuGet Packages for Solution: now we search and add the Xamarin.Auth package for all projects. 9 The next step is to configure initialize this library in every platform specific project because, as we said earlier, it has specific implementation for every OS.

Android

For the Android project we edit the MainActivity.cs file and we add this line of code:

global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, bundle);

15.png

iOS

For the iOS project the concept is the same but we edit the AppDelegate.cs class with this line of code:

global::Xamarin.Auth.Presenters.XamarinIOS.AuthenticationConfiguration.Init();

15

UWP

The same concept is valid for the UWP world with this line of code in the App.xaml.cs file.

Xamarin.Auth.Presenters.UWP.AuthenticationConfiguration.Init();

17

Linq to Twitter

To avoid the heavylifting of maning the Twitter API calls we need the help of another library that enables us to access Twitter data like a database context: this library is very powerful and well done and it’s called Linq to Twitter. The .NET Standard version of this library is not available as a NuGet package at the moment of this writing but we can compile it by cloning the repository and build the .Net Standard project. I did this and the library is available here for download. Now we add a reference to this library into the shared project. We right-click the project, Add -> Reference -> Browse and we navigate to the file system location where we compiled or downloaded the library. 20 13 After that we added the library we click OK. The final result is something like the followin image. 14 Warning! Important step! Now we try tu build our solution to check if everything is fine. If we run into this error we need to manually add another NuGet Package to all the projects of the solution: PCLCrypto (it is a dependency of Xamarin.Auth). The same may apply for Microsoft.Bcl.Build.10 If we try to compile again everything should be fine, now.

Views

Now it time to code! We start with the UI and we open the MainPage.xaml file in the Views folder of the shared library project. We write the XAML to achieve this layout. xwitterScreenshot We want to display our timeline as a list of tweets with the text, the author and the profile image. At the bottom we want a place to input some text to do a tweet and then press a button to publish it. [code language=“xml”]