xamarin - Can I use C# code to add a call to a command instead of a <Grid.GestureRecognizers>? -
i have code:
<viewcell x:name="co"> <grid verticaloptions="centerandexpand" padding="20, 0"> <grid.gesturerecognizers> <tapgesturerecognizer command="{binding openpickercommand}" commandparameter="{x:reference copicker}" numberoftapsrequired="1" /> </grid.gesturerecognizers> <picker x:name="copicker" isvisible="false" horizontaloptions="end" selectedindexchanged="copickerselectedindexchanged" itemssource="{binding order}"></picker> <label x:name="colabel" horizontaloptions="end"/> </grid> </viewcell>
is there way can in c# connect command tapping of cell rather have use xaml <grid.gesturerecognizers>
?
adding gesturerecognizer
viewcell
big no-no. viewcell
exists within listview or tableview have more enough tapped options of own. adding gesturerecognizer
might confuse os tap should handle.
your options gesturerecognizer
following 3, advise against them in scenario have listview
/tableview
.
check out of listview/viewcell based alternatives mention below in situation.
1. gesturerecognizer - add in code
var tapgesturerecognizer = new tapgesturerecognizer(); tapgesturerecognizer.tapped += (s, e) => { // handle tap }; mygrid.gesturerecognizers.add(tapgesturerecognizer);
2. gesturerecognizer - use command
when use mvvm can use command binding in c#:
var tapgesturerecognizer = new tapgesturerecognizer(); tapgesturerecognizer.setbinding (tapgesturerecognizer.commandproperty, "tapcommand"); mygrid.gesturerecognizers.add(tapgesturerecognizer);
which can bound in xaml:
<grid> <grid.gesturerecognizers> <tapgesturerecognizer command="{binding tapcommand}" /> </grid.gesturerecognizers> </grid>
3. gesturerecognizer - add in xaml have done
<grid> <grid.gesturerecognizers> <tapgesturerecognizer command="{binding openpickercommand}" commandparameter="{x:reference copicker}" numberoftapsrequired="1" /> </grid.gesturerecognizers> </grid>
4. viewcell - tapped event
for viewcell
have tapped
event:
<viewcell height="100" tapped="ontapped"> <viewcell.view> <stacklayout backgroundcolor="white" > </stacklayout> </viewcell.view> </viewcell>
which can implement in code-behind:
void ontapped (object sender, system.eventargs e) { //your code}
5. viewcell - tapped command
when using mvvm don't want put lot of business logic in code-behind pages. in case can use behavior
convert event command. sample of can found here:
6. listview - itemselected
listview
has itemselected
event. can handled same way viewcell
tapped
event both event in code-behind or behavior
delegate command
.
7. listview - selecteditem property
you can bind selecteditem
property in view model. on setter can perform custom code.
<listview itemssource="{binding youritems}" selecteditem="{binding yourselecteditem, mode=twoway}" > </listview>
and in code:
string _yourselecteditem; public string yourselecteditem { { return _yourselecteditem; } set { _yourselecteditem = value; // perform custom functionality } }
Comments
Post a Comment