I have defined a model as follows:
- Xamarin Forms Passing Method As Parameter In Java
- Xamarin Forms Passing Method As Parameter Formula
- Xamarin Forms Passing Method As Parameter In Math
Based on the above model, I have instantiated a list that contains hard coded values for testing purposes.
Further, I am dynamically populating buttons based on the TransportModel values on a XAML page as follows:
Xamarin Forms Passing Method As Parameter In Java
And I have the following ViewModel:
The issue that I am having is that I want to pass two parameters in the
CommandParameter
. And I want to access these values within the Show
method. Could someone please help me on this ? I'm totally stuck.
ExpertGenieExpertGenie
1 Answer
since each button only applies to a specific
Transport
, you want to pass a Transport
object as your CommandParameter
Teenage mutant ninja turtles vector images.then
JasonJason54.7k1313 gold badges9797 silver badges122122 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged c#xamarinxamarin.forms or ask your own question.
-->This article demonstrates using the XAML attributes that can be used to pass arguments to non-default constructors, to call factory methods, and to specify the type of a generic argument.
Overview
It's often necessary to instantiate objects with constructors that require arguments, or by calling a static creation method. This can be achieved in XAML by using the
x:Arguments
and x:FactoryMethod
attributes:- The
x:Arguments
attribute is used to specify constructor arguments for a non-default constructor, or for a factory method object declaration. For more information, see Passing Constructor Arguments. - The
x:FactoryMethod
attribute is used to specify a factory method that can be used to initialize an object. For more information, see Calling Factory Methods.
In addition, the
x:TypeArguments
attribute can be used to specify the generic type arguments to the constructor of a generic type. For more information, see Specifying a Generic Type Argument.Passing Constructor Arguments
Arguments can be passed to a non-default constructor using the
x:Arguments
attribute. Each constructor argument must be delimited within an XML element that represents the type of the argument. Xamarin.Forms supports the following elements for basic types:x:Object
x:Boolean
x:Byte
x:Int16
x:Int32
x:Int64
x:Single
x:Double
x:Decimal
x:Char
x:String
x:TimeSpan
x:Array
x:DateTime
The following code example demonstrates using the
x:Arguments
attribute with three Color
constructors:The number of elements within the
x:Arguments
tag, and the types of these elements, must match one of the Color
constructors. The Color
constructor with a single parameter requires a grayscale value from 0 (black) to 1 (white). The Color
constructor with three parameters requires a red, green, and blue value ranging from 0 to 1. The Color
constructor with four parameters adds an alpha channel as the fourth parameter.The following screenshots show the result of calling each
Color
constructor with the specified argument values:Calling Factory Methods
Factory methods can be called in XAML by specifying the method's name using the
x:FactoryMethod
attribute, and its arguments using the x:Arguments
attribute. A factory method is a public static
method that returns objects or values of the same type as the class or structure that defines the methods.The
Color
structure defines a number of factory methods, and the following code example demonstrates calling three of them:The number of elements within the
x:Arguments
tag, and the types of these elements, must match the arguments of the factory method being called. The FromRgba
factory method requires four Int32
parameters, which represent the red, green, blue, and alpha values, ranging from 0 to 255 respectively. The FromHsla
factory method requires four Double
parameters, which represent the hue, saturation, luminosity, and alpha values, ranging from 0 to 1 respectively. The FromHex
factory method requires a String
that represents the hexadecimal (A)RGB color.The following screenshots show the result of calling each
Color
factory method with the specified argument values:Specifying a Generic Type Argument
Generic type arguments for the constructor of a generic type can be specified using the
x:TypeArguments
attribute, as demonstrated in the following code example:The
OnPlatform
class is a generic class and must be instantiated with an x:TypeArguments
attribute that matches the target type. In the On
class, the Platform
attribute can accept a single string
value, or multiple comma-delimited string
values. In this example, the StackLayout.Margin
property is set to a platform-specific Thickness
.Summary
This article demonstrated using the XAML attributes that can be used to pass arguments to non-default constructors, to call factory methods, and to specify the type of a generic argument.
Related Links
NavigateTo<T>()
function located in App.csItemListViewModel.cs
DataCaptureViewModel.cs
BaseViewModel.cs
I've been developing an Xamarin.Forms application using the MVVM pattern and calling
App.NavigateTo<T>()
when needing to navigate to another page. I've now come to a point where I'd like to pass an ObservableCollection<Item>
into the page that I'm navigating to. I haven't been able to figure out how to do this using the NavigateTo<T>()
method. I've asked about in the WPF
and C#
chat circles but haven't come to a solution yet. Any ideas?Please let me know if showing more code is necessary.
HappyCoding
HappyCodingHappyCoding
1 Answer
Option-1. Use attached property
How it works: In order to pass some navigation parameters - you can store the state for a view (and the view-model) using an attached property. We keep the binding-mode as 'OneWayToSource' - so that once bound - it only updates the ViewModel, not the attached property on View itself;
This way, once
NavigateTo
method explicitly sets the value on the attached property, and we set binding on view; as soon as viewmodel is assigned to view's BindingContext
- viewmodel gets updated with the attached value due to the binding.Steps:
Create an attached bindable property.
Xamarin Forms Passing Method As Parameter Formula
Refactor
NavigateTo
to accept a parameter, and assign property value. Make sure to create a binding to property in (DataCapture) view XAML. For example:
Or, option-2. Use virtual method in BaseViewModel
Refactor
NavigateTo
to accept a parameter, and update view-model. Either reuse
OnAppearing
, or create a method like SetNavigationContext
in your BaseViewModel
Sure cuts alot 2 free download full version full.Override this method in
G.SharadaG.SharadaDataCaptureViewModel
11.2k11 gold badge1212 silver badges3636 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged c#mvvmxamarin.forms or ask your own question.
I am adding some pins to a map, and when the user tap on this pin (actually the content of the pin) I want to open a specific page.
I want to do something like this:
But I don't know if it is possible to pass parameters to the
OnPinClicked
function. Is that possible? If it is not, what can I do to solve this?Note: I'm newbie on Xamarin and C#.
KelvinS
KelvinSKelvinS
3 Answers
You can't pass arguments to event handlers.
Xamarin Forms Passing Method As Parameter In Math
You can write wrapper(decorator) for
Pin
class:Then use this class in
PopulateMap()
method:And your click handler:
OR
You can assign handler by another way:
AFTER QUESTION EDIT:
There is a closure. You should create new variable and use it inside handler.
Roman DoskochRoman Doskoch9,5161010 gold badges2828 silver badges3939 bronze badges
mdimai666mdimai666
You can create it directly using a short coding version of
ZroqZroqEventHandler
, which on your case is the same thing as calling OnPinClicked
:3,50622 gold badges1212 silver badges2121 bronze badges