XamarinT Dark_870x220

Sometimes, we need to find a way to pass some parameters from the XAML to our code behind it, but we just don’t know how to do it. So that’s why in this article we will be learning how easy it is to pass arguments to our controls in XAML. We’ll explore different ways, such as: Passing arguments to non-default constructors, calling factory methods and generic arguments.

The topics we will explain include:

  • Passing arguments
    • Constructor arguments
    • Factory methods
    • Generic methods
  • Data types allowed

📝 Learning to Pass Arguments

Constructor Arguments

Yes! 😎 Constructor arguments can be passed from the XAML. Xamarin.Forms allows us to pass these types or arguments through the XAML page.

Let’s see the structure we need to use!

				    <x:ArgumentTtype>Your vaule </x:ArgumentType>
				    <!--Add all the argument that you need-->

Imagine that you have to fill the Color’s constructor for a BoxView’s Color property. There are various constructors within the class Color, and one of them gets four values which are Double type. With the structure explained above we just have to do something like this:

    <BoxView HeightRequest="150" WidthRequest="150" HorizontalOptions="Center">  

Factory Methods

And if the previous was not enough, with this way of passing arguments, we can also access the factory methods! But first… What are Factory methods? 🤔 A factory method is a public and static method that returns objects of the same type of parent class. To use it, you just have to add the x:FactoryMethod attribute to the same structure explained in the constructor argument. Right next to the property control UI tags, as I show below:

    <Color x:FactoryMethod="FromHex">

An example to use:

<BoxView HeightRequest="150" WidthRequest="150" HorizontalOptions="Center">
        <Color x:FactoryMethod="FromHex">


Generic Type Arguments

Finally, we can pass the argument of a generic type. We just have to use the x:TypeArguments attribute, and use the structure as in the following example:
      <OnPlatform x:TypeArguments="Thickness">
        <On Platform="iOS" Value="0,20,0,0" />
        <On Platform="Android" Value="5, 10" />
        <On Platform="UWP" Value="10" />

🔑 Data Types Allowed

This is a great way to pass arguments from the XAML, but it’s important to know that we have some limitations of the data types that the arguments support. The following table contains all the types that are supported:

Object   Boolean   Byte   Single  Array 
 Int16  Int32   Int64  Double  DateTime
 Decimal  Char  String  TimeSpan  


📒 Note: When using any of these data types in an argument, you must use the “x:” namespace and the value of the argument must have the following structure:



- https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/passing-arguments

About the Author

Leomaris Reyes

Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of  Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.

Related Posts


Comments are disabled in preview mode.