android - Tap Gesture Gesture Recognizer in ListView not working -


i have in viewmodel:

public class myclass: inotifypropertychanged {     public event propertychangedeventhandler propertychanged;     int taps = 0;     icommand tapcommand;      public myclass()     {         tapcommand = new command(ontapped);     }      public icommand tapcommand     {         { return tapcommand; }     }      void ontapped(object s)     {         taps++;         debug.writeline("parameter: " + s);     } } 

and in xaml:

<image source="delete.jpg" heightrequest="20" widthrequest="20">      <image.gesturerecognizers>         <tapgesturerecognizer             command="{binding tapcommand}"             commandparameter="image1" />     </image.gesturerecognizers>  </image> 

but when image clicked nothing appears in output log. i'm missing ?

note 1: i've followed guide here

note 2: i'm debugging on android device

update 1: full xaml here

you have not provided code behind, had create own (see below). save me 15 minutes if commented out listview isenabled="false"

when set command="{binding tapcommand} tapcommand corresponds tapcommand of list item, not model itself. so, have 2 options

  1. you can implement click in myitem (i don't think want that, partial code commented below in myitem class)
  2. define context binding on image itself. because of our view model can created few times - don't want that, best solution define static resource of view model , use everywhere on page. solution below (you need modify namespaces , change delte.png delete.jpg):

page xml

<?xml version="1.0" encoding="utf-8" ?> <contentpage xmlns="http://xamarin.com/schemas/2014/forms"              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              xmlns:local="clr-namespace:buttonrendererdemo;assembly=buttonrendererdemo"              x:class="buttonrendererdemo.imagetapcomplexpage"              bindingcontext="{staticresource viewmodel}">        <contentpage.resources>         <resourcedictionary>           <local:tapcomplexviewmodel x:key="viewmodel"/>         </resourcedictionary>       </contentpage.resources>    <stacklayout>     <label text="text2" verticaloptions="center" horizontaloptions="center" />     <stacklayout padding="0,20,0,20">     <button text="text" clicked="onclick" backgroundcolor="#009688"></button>      </stacklayout>     <label text="list type" verticaloptions="center" horizontaloptions="center" />     <listview x:name="lstitems" rowheight="60" itemssource="{binding items}" > <!--isenabled="false"-->       <listview.itemtemplate>         <datatemplate>           <viewcell>             <stacklayout orientation="horizontal" horizontaloptions="fill" padding="0,2,0,2">               <stacklayout padding="5,5,5,5">                 <image source="{binding source}" horizontaloptions="end" heightrequest="40" widthrequest="40" />               </stacklayout>               <stacklayout orientation="vertical" spacing="1">                 <label text = "{binding name}" heightrequest="20" fontattributes="bold"/>                 <label text = "{binding data, stringformat='{0:f0}'}" />               </stacklayout>               <stacklayout horizontaloptions="endandexpand" padding="0,0,15,0" orientation="horizontal">                 <image source="delete.png" heightrequest="20" widthrequest="20">                    <image.gesturerecognizers>                     <!--<tapgesturerecognizer                          command="{binding tapcommand}"                         commandparameter="image1" />-->                         <tapgesturerecognizer command="{binding source={staticresource viewmodel}, path=tapcommand}" commandparameter="{binding name}" />                  </image.gesturerecognizers>                 </image>               </stacklayout>             </stacklayout>           </viewcell>         </datatemplate>       </listview.itemtemplate>     </listview>   </stacklayout> </contentpage> 

code behind

namespace buttonrendererdemo {     public partial class imagetapcomplexpage : contentpage     {         public imagetapcomplexpage()         {             initializecomponent();                 }          void onclick(object sender, eventargs args)         {          }     }      public class tapcomplexviewmodel : inotifypropertychanged     {         public event propertychangedeventhandler propertychanged;         int taps = 0;         icommand tapcommand;          public observablecollection<myitem> items { get; private set; }         public tapcomplexviewmodel()         {             items = new observablecollection<myitem>()             {                 new myitem { name="first", source="icon.png", data=0.5f },                 new myitem { name="second", source="icon.png", data=0.6f },                 new myitem { name="third", source="icon.png", data=0.7f }             };              tapcommand = new command(ontapped);         }          public icommand tapcommand         {             { return tapcommand; }         }          void ontapped(object s)         {             taps++;             debug.writeline("parameter: " + s);         }        }      public class myitem     {         public string name { get; set; }         public string source { get; set; }         public float data { get; set; }          //public icommand tapcommand         //{         //    { return new command(() => { }); }         //}     } } 

enter image description here


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -