Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Three answers below ... I prefer the one marked <strong>3</strong></p> <h3>1) A Simpler Approach</h3> <p>At a really basic level, I think you can just use the built-in binding for this.</p> <p>Looking at <code>ItemClick</code> in <a href="https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBindableGridView.cs" rel="nofollow noreferrer">https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBindableGridView.cs</a> you should be able to just do:</p> <pre><code>&lt;cirrious.mvvmcross.binding.android.views.MvxBindableGridView android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="4" android:gravity="center" android:listSelector="#00000000" local:MvxItemTemplate="@layout/itemimage" local:MvxBind="{'ItemsSource':{'Path':'Squares'}, 'ItemClick':{'Path':'ClickCommand'}}" /&gt; </code></pre> <p>where <code>ClickCommand</code> is something like:</p> <pre><code>public ICommand ClickCommand { get { return new MvxRelayCommand&lt;Square&gt;(square =&gt; square.Foo()); } } </code></pre> <p>If this doesn't work, then please <a href="https://github.com/slodge/MvvmCross/issues/new" rel="nofollow noreferrer">raise a bug with test code</a> - the grid was community donated to MvvmCross so it's not something I've used yet.</p> <hr> <h3>2) To make your custom binding work</h3> <p>If you do want to do the custom binding.</p> <p>Using Intellisense, it looks like an <code>AdapterView.ItemClickEventArgs</code> has a couple of potentially useful properties:</p> <pre><code> e.View; e.Position; </code></pre> <p>So you can access the <code>View</code> if you want to - and can then use things like <code>FindViewById&lt;&gt;</code> to find contained views.</p> <p>Or you can access the <code>Position</code> and can then: - use Adapter methods like <code>public override Object GetItem(int position)</code> to get a Java wrapped object from your original array - or can use accessor method direct on your incoming array or enumerable.</p> <hr> <h3>3) The way I normally work</h3> <p>I generally don't do this sort of custom binding when all I want to do is click on a list item or part of a list item. </p> <p>Instead I write my ViewModels so that they expose behaviour-enabled collections - I try to present a behaviour-enabled wrapper of the Model object rather than the Model object itself.</p> <p>In this approach the ViewModel exposes a <code>List&lt;WrappedSquare&gt;</code> instead of a <code>List&lt;Square&gt;</code></p> <pre><code>public WrappedSquare { Square _saure; SquareViewModel _square; public WrappedSquare(Square square, SquareViewModel parent) { /* assignment */ } public ICommand TheCommand { get { return MvxRelayCommand(() -&gt; _parent.DoStuff(_square)); } } public Square TheSquare { get { return _square; } } } </code></pre> <p>The axml within the list item then has bindings like:</p> <pre><code>&lt;ImageView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res/LeSommet.ZooSnap.UI.Android" android:layout_width="80dp" android:layout_height="80dp" android:padding="5dp" android:layout_gravity="center" local:MvxBind="{'ResourcesImagePath':{'Path':'TheSquare.ImagePath'},'Click':{'Path':'TheCommand'}}" /&gt; </code></pre> <p>and the axml for the grid is:</p> <pre><code>&lt;cirrious.mvvmcross.binding.android.views.MvxBindableGridView android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="4" android:gravity="center" android:listSelector="#00000000" local:MvxItemTemplate="@layout/itemimage" local:MvxBind="{'ItemsSource':{'Path':'WrappedSquares'}}" /&gt; </code></pre> <p>This example code is adapted from <a href="https://stackoverflow.com/questions/12682082/mvvmcross-changing-viewmodel-within-a-mvxbindablelistview">MVVMCross changing ViewModel within a MvxBindableListView</a> - the answer there is relevant to this question too.</p> <hr> <p>One real example of this approach is the Conference sample which uses <code>WithCommand&lt;T&gt;</code> to enable navigation from lists to detail views.</p> <p>However... there is one warning on this approach - please note that when using <code>WithCommand&lt;T&gt;</code> we discovered a memory leak in iOS/MonoTouch - basically the GarbageCollection refused to collect the embedded <code>MvxRelayCommand</code> - which is why <code>WithCommand&lt;T&gt;</code> is <code>IDisposable</code> and why <code>BaseSessionListViewMode</code> clears the list and disposes the <code>WithCommand</code> elements when views are detached. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload