When passing arguments by the markup extension x:Arguments
to non default constructors as specified by the docs, I can see the use of concrete data types such as x:Int32
or x:String
, but what's the use case of x:Object
? And what's more, to use it, what should be put between the tag? <x:Object> ??? </x:Object>
In the case of integer or string, it is natural to think them as an variable assignment and the variables then get passed to the constructors. But in the case of Object
, such an variable normally is constructed by another user defined class, so how to specify what class you want to create?
The x:Object primitive corresponds to Object. This primitive is not typically used in application markup but might be useful for some scenarios such as checking assignability in a XAML type system. You can use as Argument.
Take a look at the Xamarin documentation on Resource Dictionaries for a full explanation on how to use the x:Key attribute of your resources.
Each resource has a key that is specified using the x:Key attribute, which gives it a descriptive key in the ResourceDictionary.
You can use it like below:
<local:MockFactory >
<x:Arguments>
<x:Array Type="{x:Type x:Object}">
<x:String>Foo</x:String>
<x:String>Bar</x:String>
</x:Array>
</x:Arguments>
</local:MockFactory>
you can find a related example here
Technically you can pass x:Object
as constructor argument. x:Object
corresponds to System.Object
which does have default constructor. So if you have
public class MyClass {
public MyClass(object arg) {
}
}
You can construct it like this:
<my:MyClass>
<x:Arguments>
<x:Object />
</x:Arguments>
</my:MyClass>
But this is not quite useful, because it will correspond to
new MyClass(new object());
Since Object
does not have any other constructors - you cannot construct it in any other meaningful way. So when constructor expects argument of type object - you don't want to use x:Object
but instead actual type:
<my:MyClass>
<x:Arguments>
<x:String>string</x:String>
</x:Arguments>
</my:MyClass>
which corresponds to
new MyClass("string");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With