PROWAREtech

articles » archived » xamarin » xaml-versus-code

Xamarin: XAML versus Code

XAML or code - which to choose.

Xamarin applications written entirely in C# code are stringy and not very elegant. They are also more difficult to be parsed by software tools than XAML.

XAML allows separating the developer and designer. Designers use their tools to generate XAML while programmers can focus their efforts on coding to the XAML. XAML equals the visuals of the application while C# code equals the logic.

The verbose nature of XAML does make writing it by hand (not using a designer like Blend) more difficult. As the the writing of this article, there is no XAML designer for Xamarin.Forms.

What can be done in code that cannot be done in XAML? One cannot create widgets/controls dynamically using XAML. This must be done in the code. But this behavior is rare.

A button example in C#:

new Button
{
	IsVisible = true,
	Text = "Click Me!",
	Opacity = 0.85,
	TextColor = Color.FromRgb(0, 0, 128),
	BackgroundColor = Color.FromRgb(128, 128, 128),
	FontSize = Device.GetNamedSize(NamedSize.Large,	typeof(Button)),
	FontAttributes = FontAttributes.Italic | FontAttributes.Bold
};

Now the equivalent in XAML:

<Button
	IsVisible="True"
	Text="Click Me!"
	Opacity="0.85"
	TextColor="#000080"
	BackgroundColor="#808080"
	FontSize="Large"
	FontAttributes="Italic,Bold" />

Clearly, there is less code in the XAML example.



This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE