xamarin - How can I make the Tapped event of a ViewCell send a param to a generic function and then open up a picker for that ViewCell element? -
update: reminder, there's 500 point bonus on if can show me how implement functionality without using gestures>
i using viewcell , gesture recognizer open picker following code. viewcell has label on left , label area on right populated when app starts , later picker when viewcell clicked.
xaml
<viewcell x:name="ati" tapped="openpickercommand"> <grid verticaloptions="centerandexpand" padding="20, 0"> <grid.gesturerecognizers> <tapgesturerecognizer command="{binding openpickercommand}" commandparameter="{x:reference atipicker}" numberoftapsrequired="1" /> </grid.gesturerecognizers> <local:labelbodyrendererclass text="answer time interval" horizontaloptions="startandexpand" /> <picker x:name="atipicker" isvisible="false" horizontaloptions="end" selectedindexchanged="atipickerselectedindexchanged" itemssource="{binding times}"></picker> <local:labelbodyrendererclass x:name="atilabel" horizontaloptions="end"/> </grid> </viewcell> <viewcell x:name="pti" tapped="openpickercommand"> <grid verticaloptions="centerandexpand" padding="20, 0"> <grid.gesturerecognizers> <tapgesturerecognizer command="{binding openpickercommand}" commandparameter="{x:reference ptipicker}" numberoftapsrequired="1" /> </grid.gesturerecognizers> <local:labelbodyrendererclass text="phrase time interval" horizontaloptions="startandexpand" /> <picker x:name="ptipicker" isvisible="false" horizontaloptions="end" selectedindexchanged="ptipickerselectedindexchanged" itemssource="{binding times}"></picker> <local:labelbodyrendererclass x:name="ptilabel" horizontaloptions="end"/> </grid> </viewcell>
c# works different pickers (ati, bti, pti etc) commandparameter
public settingspage() { initializecomponent(); bindingcontext = new commandviewmodel(); } void atipickerselectedindexchanged(object sender, eventargs e) { var picker = (picker)sender; int selectedindex = picker.selectedindex; if (selectedindex != -1) { app.db.updateintsetting(settings.ati, selectedindex); atilabel.text = as.ati.text(); } } void ptipickerselectedindexchanged(object sender, eventargs e) { var picker = (picker)sender; int selectedindex = picker.selectedindex; if (selectedindex != -1) { app.db.updateintsetting(settings.pti, selectedindex); ptilabel.text = as.pti.text(); } } public class commandviewmodel: observableproperty { public icommand openpickercommand; public commandviewmodel() { openpickercommand = new command<picker>(pickerfocus); //openpickercommand = new command(tapped); } public icommand openpickercommand { { return openpickercommand; } } void pickerfocus(picker param) { param.focus(); } }
i remove use of tapgesturerecognizers still want retain functionality , layout.
it's been suggested me better if used tapped event of viewcell this:
tapped="ontapped"
can explain in detail how wire in c#. best code commandviewmodel in c# backing code. can view model have 1 method takes argument used open different pickers?
an example of how appreciated. note don't particularly need use commandviewmodel if there way coding in .cs backing code.
(sorry poor english)
despite not being best practice, guess can this, dismissing viewmodel:
xaml:
<viewcell x:name="ati" tapped="openpickercommand"> <grid verticaloptions="centerandexpand" padding="20, 0"> <local:labelbodyrendererclass text="answer time interval" horizontaloptions="startandexpand" /> <picker x:name="atipicker" isvisible="false" horizontaloptions="end" selectedindexchanged="atipickerselectedindexchanged" itemssource="{binding times}"> </picker> <local:labelbodyrendererclass x:name="atilabel" horizontaloptions="end"/> </grid> </viewcell> <viewcell x:name="pti" tapped="openpickercommand"> <grid verticaloptions="centerandexpand" padding="20, 0"> <local:labelbodyrendererclass text="phrase time interval" horizontaloptions="startandexpand" /> <picker x:name="ptipicker" isvisible="false" horizontaloptions="end" selectedindexchanged="ptipickerselectedindexchanged" itemssource="{binding times}"></picker> <local:labelbodyrendererclass x:name="ptilabel" horizontaloptions="end"/> </grid> </viewcell>
c#:
private void openpickercommand(object sender, system.eventargs e) { if (sender != null) { picker pkr = sender == ati ? atipicker : ptipicker; pkr.focus(); } }
answering question "can view model have 1 method takes argument?", you're doing using 'openpickercommand' method. problem using viewcell's public event 'tapped', can't set parameters delegate handler.
let me know if works or if need more information.
i hope helps.
Comments
Post a Comment