Friday, 21 February 2020

Xamarin Coding Standards

There are many best practices in writing Xamarin. Here are some that we’ve canonized where I work…
  •       By convention the identifier for the ViewModel is vm
  •       Do not assign more than one page to a view model. Generally speaking: it should be one page to one view model
  •     Do not pass view modes to methods of other pages
 The only code in code-behind (e.g., foo.xaml.cs) should be
  • initialize in the constructor
  • create the view model
  • assign the datacontext to the view model
  •  call the viewmodel’s Initialize method
·         Lifecycle methods such as OnAppearing and OnDisappearing

    Why: we want to keep the code behind as sparse as possible. Unit testing requires reaching into code and that is infinitely easier with a viewmodel.

Every ViewModel should have an initialize method.

    Why: Having an initialize method keeps most of the code for the vm out of the constructor. This is recommended practice by Microsoft, and allows for async methods.

Do not put Xaml in templates in App.xaml. Put the Xaml in the Xaml file.

    Why: App.xaml quickly becomes bloated and hard to work with. Having the Xaml in the Xaml file is natural and helps create the troika we want: foo.xaml, foo.xaml.cs and fooViewModel.cs.

Create viewmodel name by appending “viewmodel” to the xaml name

  • LigthController.xaml
  • LightController.xaml.cs
  • LightControllerViewModel.cs

    Why: It is far easier to find the file you want if we follow a convention.

Avoid Xaml file names ending in “view”

  • LightControllerView.xaml
  • LightControllerView.xaml.cs
  • LightControllerViewViewModel.cs

    Why: Adding view to a view file is redundant and it makes reading the name of the ViewModel more difficult.

Use commands rather than event handlers

// wrong - handled in code behind
<button Text="Divide by 2" Clicked="onClick" /> 
//correct - handled in viewmodel
<button Text="Divide by 2" ClickedCommand="{Binding DivideBy2Command"
 
Why: It is much easier to write unit tests when the event handler is in the viewmodel.

Generate a PDF File from HTML String

XFPDF

This repository demonstrates you to generate the PDF file from HTML string in Xamarin Forms without any third party package.
In Xamarin Forms, there is no default support either to generate the PDF or to view the PDF file. But you can achieve these requirements with native support through renderers for both Android and iOS platform.
The demo application in the repository is divided into three segments.
  1. Create HTML from URL.
  2. Convert the HTML string to PDF file.
  3. View the PDF file.
GitHub Link

Wednesday, 1 January 2020

How To Keep Your Android App Size Down

How To Keep Your Android App Size Down 

There are a few ways you can take to make your app smaller when developing with Xamarin. 
Additionally, any Android developer can do one simple thing when deploying to Google Play to reduce
their app size.

Linking Your Libraries


Xamarin applications use a “linker” in order to reduce your app size. 
You can browse through the documentation and find out how this works, but to simplify things,
it uses static analysis to your app to remove assemblies and types that are not used in your app to 
bring down your app size. This is for any Xamarin app, so you should also try this out in your iOS app 
because it can reduce your app size in a default “Hello, World” 
application from 16MB down to 2.9MB! There are three settings that you can supply from the
projects settings:

Don’t Link will do just that, it won’t link anything and you will be left with All of Mono, mscorlib, 
Xamarin.Android, and a bunch of other stuff:

Link SDK assemblies only is your safest bet and should be your default as it will only
attempt to strip things out of Xamarin.Android and any of your third party libraries will not be
touched. However, to really bring down your app size you should try out Link All Assemblies,
as it will investigate everything and bring down your app size. Be sure to FULLY test your app 
as it is possible that the linker may be agressive and strip out something you need, and if that is the 
case you can actually use a [Android.Runtime.Preserve] flag or set a linkskip in your MSBuild to 
ensure that not all of your libraries get linked. 
So employing this practice with my Bike Now app, which uses Json.NET, Android Support v4, v7,
Google Play Services, and Xamarin.Insights, we can compare and contrast the app size when we 
build our app to support all three ABIs (we will talk about this next!).
  • Don’t Link: 40.7MB
  • Link SDK Assemblies Only: 18.7MB
  • Link All Assemblies: 13MB
As you can see linking correctly can make a huge impact, but we can do even better!

Splitting your APKs

On Android, there are ABIs (Application Binary Interfaces) that you can support when you
ship your application. The most used will be the armeabi-v7a, however there are still tons of devices 
that support and run the old armeabi ABI and even x86 devices as well. So to ensure your app is
reaching the most users you most likely have come into the project settings and selected every single
ABI
 
However, for every ABI that you select you are actually bundling a separate lib monodroid and 
sgen with your app. 
Don’t believe me then rename your .apk to .zip and take a look in the lib folder:

This of course makes sense as you would need a different version of monodroid and sgen 
that supports that ABI. The issue is that you now have all of these libraries bundle into a single APK
and your users will be downloading all of them! The solution for any Android developer 
(even Java devs) is to simply split up your APKs and upload all of them to Google Play! 
This way you have a smaller app size across all three APKs. 
You can do this now with a simple check in your project options:

Now, instead of just a single APK to upload I have three with different and smaller sizes
 (note it will take longer to create your packages):
  • armeabi-v7a: 10.2MB
  • armeabi: 10.3MB
  • x86: 10.4MB
Notes: 
You may need to close XS after selecting check box and ensure this flag is set in your csproj:
<AndroidCreatePackagePerAbi>true</AndroidCreatePackagePerAbi> 
Additionally, your new APKs will be in your /bin/Release folder and will be marked with Signed in
 their file name.

Keep your users happy and keep down that app size with these quick tips.

Thursday, 19 September 2019

Xamarin.Forms - Implement CI/CD Using App Center

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that 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.

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.

Connect your repo, build your app. It’s that simple.

Connect to GitHub, Bitbucket, or Azure DevOps and build your app in the cloud on every commit.
Automatically run unit tests, release to testers and stores, or test your UI on real devices.
 


Support Platforms

  • Android
  • iOS
  • React Native
  • UWP
  • Xamarin
  • macOS
  • Cordova
 

App Center Services

  1. Build
  2. Diagnostics (Formerly Crashes)
  3. Test
  4. Analytics
  5. Distribute
  6. Push Notifications
CI - Continuous integration is the process of merging all developers' work into a single main repository.
 
CD - Continuous delivery is a process of automatically building and deploying your Xamarin apps to testers or end-users.
 
More info
 
 

 

Tuesday, 26 March 2019

Common Challenges Faced by Xamarin.Forms Beginners and Possible Solutions



1 – I get a thousand error messages at my first build 
This is one of the most annoying challenge a Xamarin Forms beginner can face. You launch your IDE, Select Xamarin.Forms project template, but you can’t run it immediately. This is either due to nuget packages which are not restored or packages which need to be updated. If you face this issue, the first thing to do is to restore all your nuget packages for the solution and update if need be. Check this article on how to restore nuget packages in visual studio. 


2- I can’t compile and run my app!!! 
In some cases, even after restoring packages you may have difficulties compiling and running your app. This is usually because of platform specific packages needed to run the app. For example, after restoring nuget packages,  you may face errors like missing package Xamarin.Android.Support.Design or any other Xamarin.Forms support package issue. Your IDE may complain about Missing References to Dependencies or Packages which are actually PRESENT in your solution Or, after running you app, it emidiately closes. All of these issues and a lot more will hinder compilation. Resolving such issue is not always easy, and covering how to deal with each of them requires me to write a new post so instead, here is a link to solutions to these commonly faced problems. 


3- What the hell is this XAML stuff ? Why does this XAML seem so different from .Net XAML 
When you are a Xamarin.Forms beginner, and new to .Net development too, you can get very confused in the beginning. XAML is a markup language used to build user interfaces in .Net apps. You have the choice to build user interfaces either with XAML or with C# code and you may ask your self if you should use XAML the answer is YES you should. Using XAML allows you to better separate UI code from code logic, and is a better choice when you will implement MVVM. Another difficulty comes when you are from WPF, UWP … background you think the XAML you are used to is another form of XAML, with different names for controls. to overcome this, You should check Xamarin.Forms documentation, and Check this article about XAML Standards. 


4- Where are these Ghost exceptions coming from ? How do I understand them ? 
Another annoying issue is, running your app to later have it throw what I call Ghost Exceptions Exceptions thrown with only a minimum information about their occurrence. For example, only the name of the exception is given, and but no additional information is given. This type of exception occures mostly when there is an error in your XAML code, and since you cannot place break points in XAML, it is a little bit difficult to figure out what the exception is. The best way I overcome this is by launching the app on a different platform, and depending on how the exception is thrown on each platform, you can get enough information about it. I mostly find out that such exceptions are thrown when ever I’m careless in my XAML code. 


5- Where is the designer ? 
When you are from a mobile development background like Android or UWP, you are used to have a designer where you drag and drop views. But in Xamarin.Forms there is no such designer at least not yet and not to my knowledge. This can seem weird at first and even frustrating, check this post from a Xamarin.Forms beginner who poured out his rage after such frustration. Check this article which will tell you more about this. 


6- Debugging Sucks !!! 
This is one of the most common complaints. You want to debug your app, and you find out that it is some times not as easy as what you are used to. Exceptions occur all the time and where in your code the exception occured is not always obvious. Yes, this is sometimes true, but Xamarin.Forms has become a lot better with time, so nowadays it is even easier to debug. My preferred debugging approaches are; When the unhandled exception occurs I read the Inner Exceptions from these exceptions. This almost always give me a clue about the cause and where the exception occurred. Or when an unknown exception fires, with no details on platforms like android, I try to run the app on windows to fire this exception, since in my opinion, windows platform describes better the exceptions which occur. Of course when I use Xamarin.Forms. 


7- Must I compile and run my code every time I want to see my app’s UI ? 
The answer to this is clear and simple. No, you mustn’t. There are several tools available for you to visualize your XAML layout while you code. Some of these codes include the XAML previewer on Visual Studio and Gorilla Player. These tools will make app development in Xamarin.Forms a lot more faster. 


8- MVVM 
When you are new to .Net platform, you may wonder what is MVVM. Because you must have heard about it several times especially if you develop mobile or desktop apps. Though MVVM Architectural Design Pattern is not a must, you should invest time learning it and master a Xamarin.Forms MVVM Framework. Which will make your code more maintainable.And you will have a better time while building Xamarin.Forms apps since it will  avoid you countless troubles related to poorly designed code. 


9- Why does my app not look totally native as it should be ? 
In deed, Xamarin.Forms is a Native cross platform UI framework. But when you take a look at the controls which it provides for each platform, they are not always identical to those you see on the respective platforms. In my Opinion, the order in which most of Xamarin.Forms controls look native on each platform is UWP > Android > iOS. But this does not mean you cannot give this native aspect to your controls. This could be done very easily using custom renderers as described in this documentation. 


10- Understanding the Xamarin.Forms Philosophy 
This is one of the things I find in common with most of the beginners who ask questions. On sites like Reddit or Stackoverflow. They don’t fully the Xamarin.Forms approach to cross platform development. Xamarin.Forms is not a Black Box Tool which you will code everything in C# and Tada!!! is is converted into native Android, iOS … native app. Instead, you should see it as a only a UI Framework. Which provides you a set of controls you could use in one project. And these controls have their own implementation on each platform. The Xamarin.Forms way is very particular, and I think understanding it will help facilitate its use by any new developer. This article deeply explains the XAmarin.Forms way. 
 

Complete Guide: Building a Live Cricket Streaming App for 100M Users

Comprehensive guide to building a scalable live cricket streaming platform for 100M users, covering backend infrastructure, streaming techno...