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:
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!
<YourUIControl> <UIControl.Property> <Property> <x:Arguments> <x:ArgumentTtype>Your vaule </x:ArgumentType> <!--Add all the argument that you need--> <x:Arguments> </Property> </UIControl.Property> <YourUIControl>
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"> <BoxView.Color> <Color> <x:Arguments> <x:Double>0.25</x:Double> <x:Double>0.50</x:Double> <x:Double>0.9</x:Double> <x:Double>0.10</x:Double> </x:Arguments> </Color> </BoxView.Color> </BoxView>
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:
An example to use:
<BoxView HeightRequest="150" WidthRequest="150" HorizontalOptions="Center">
x:TypeArgumentsattribute, and use the structure as in the following example:
<StackLayout> <StackLayout.Margin> <OnPlatform x:TypeArguments="Thickness"> <On Platform="iOS" Value="0,20,0,0" /> <On Platform="Android" Value="5, 10" /> <On Platform="UWP" Value="10" /> </OnPlatform> </StackLayout.Margin> </StackLayout>
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:
📒 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:
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.
Subscribe to be the first to get our expert-written articles and tutorials for developers!
All fields are required
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.