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. 
 

All About .NET MAUI

  What’s .NET MAUI? .NET MAUI (.NET Multi-platform App UI) is a framework for building modern, multi-platform, natively compiled iOS, Androi...

Ads2