Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin UI: Programmatically VS XAML

recently I attended a talk on Xamarin and we were discouraged from using XAML for our UI design. I recently started mobile development and I would like to know which is the best way to design user-interfaces; doing it programmatically or using XAML?

like image 940
CodeSlave Avatar asked Mar 29 '15 07:03

CodeSlave


2 Answers

EDIT 2017 - With XAMLC (XAML-Compiler, read more here) XAML is compiled to IL code at build time so no XAML parsing is involved at runtime anymore. In many cases this makes XAML as fast or very close to coding the UI in code-behind C#. With the latest improvements in XAML compilation, there is now strongly typed data binding (read more here), which is faster and the data binding errors are caught during build time. Some errors in XAML are caught at design time, when you edit the XAML in the Visual Studio XAML editor

I assume you're asking about Xamarin Forms.

It would be interesting if you could mention the reasons why you heard XAML is bad.

The only reason I would think that someone could discourage you from using XAML is performance.

I came across some posts on Xamarin Forms talking about performance issues when using XAML for creating some of the UI with Xamarin Forms. But given than Xamarin Forms is an early technology, I guess these issues are temporary and will be fixed.

I don't agree declarative UI (XAML) vs coded UI is just a matter of personal taste.

A declarative UI (today is XAML, tomorrow it can be something else) can be a better approach than doing the UI by code, for the following reasons:

  • It's coincise, it's impersonal. Except few things, XAML looks the same for everyone. You can't say the same thing about actual code, which is more verbose, there are methods called, variables declared, etc.

  • It's more maintainable. It's easier to read and modify than actual code.

  • It helps with a clear separation of concerns, between the UI and logic. Same thing can be achieved without XAML, but just by using XAML you are closer to this goal.

  • Tooling friendly. Unfortunately Xamarin Forms today still doesn't have a previewer or designer, but this is supposed to change There is a XAML Previewer baked-in in Visual Studio when you edit XAML, but it still doesn't work 100%. See more about Xamarin Forms XAML Previewer here and here

I am NOT saying XAML is perfect. But I think it's better than manually coding the UI. XAML uses same C# classes and properties of controls. The XAML syntax is the same with XML, because actually all XAML documents are valid XML documents. XAML is based on XML.

When someone says XAML is bad, I can't help thinking about HTML. Is HTML bad too? Is it better to create HTML elements in Javascript? Obviously it's not. I know some will say you can't really compare XAML vs HTML because XAML is not as "native" as HTML, but aren't both just related to the presentation layer?

Xamarin (now Microsoft) has some nice introduction to XAML you can read here.

I think developers with native iOS background have a strong inclination towards not using a declarative UI because that's how unfortunately native iOS development mostly works. You are forced to use code to implement different things which are not available in the designer and there's no native declarative UI markup language available(there's one but the format is not public and it's not meant to be directly modified).

You might know this by now but Xamarin Forms is not the only way to build apps with Xamarin. Xamarin != Xamarin Forms. You can build your UI natively and still share the logic (ViewModels, services, etc). On Android, you can build your UI in the AXML. You will get as closer to the metal you can get with Xamarin with that. But you will need data-binding, so a framework like MvvmCross can help with that.

like image 143
WriteEatSleepRepeat Avatar answered Nov 06 '22 07:11

WriteEatSleepRepeat


A very subjective question. Currently the state of Xamarin Forms XAML is similar to early Silverlight in terms of designer support. Similarly, in Visual Studio there has been a lack of Xaml Intellisence though if you use the latest version of Resharper it will provide Intellisense for it.

It can also be frustrating to debug as the command to load the Xaml can cause a crash if the Xaml is invalid. It is still debugable if you catch the exception right there but an error in custom code would be easier to find. Finally I have heard anecdotal evidence that in some cases the XAML is slower but I have not yet confirmed it.

Having said all that, I use XAML. It tends to be the defacto standard in development with languages that have XAML. I also suspect a Xamarin Forms XAML designer will come. Like the early days of Silverlight it is my belief that using XAML may not be the easiest today but it will likely be The best option to prepare your codebase for tomorrow. Once you code your UI you will have quite the task to switch to XAML when the tooling catches up.

But as I stated earlier, a subjective question.

like image 24
Kevin Ford Avatar answered Nov 06 '22 07:11

Kevin Ford