Little problem with my Android application and I don't know how to solve it with MVVM Cross.
Here is my Model
public class Article
{
string Label{ get; set; }
string Remark { get; set; }
}
My ViewModel
public class ArticleViewModel: MvxViewModel
{
public List<Article> Articles;
....
}
My layout.axml ...
<LinearLayout
android:layout_width="0dip"
android:layout_weight="6"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/layoutArticleList">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editSearch"
android:text=""
android:singleLine="True"
android:selectAllOnFocus="true"
android:capitalize="characters"
android:drawableLeft="@drawable/ic_search_24"
local:MvxBind="{'Text':{'Path':'Filter','Mode':'TwoWay'}}"
/>
<Mvx.MvxBindableListView
android:id="@+id/listviewArticle"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
local:MvxItemTemplate="@layout/article_rowlayout"
local:MvxBind="{'ItemsSource':{'Path':'Articles'}}" />
</LinearLayout>
...
And here comes my problem, the "article_rowlayout"
...
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue">
<TextView
android:id="@+id/rowArticleLabel"
android:layout_width="0dip"
android:layout_weight="14"
android:layout_height="wrap_content"
android:textSize="28dip"
local:MvxBind="{'Text':{'Path':'Label'}}" />
<ImageButton
android:src="@drawable/ic_modify"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/rowArticleButtonModify"
android:background="@null"
android:focusable="false"
android:clickable="true"
local:MvxBind="{'Click':{'Path':'MyTest'}}"
/>
...
The "Click" command called "MyTest" is linked on the item given by the MvxBindableListView. In other words, Click search for a command "MyTest" in my model "Article", instead of my ViewModel. How can I change that behaviour in order to link my ViewModel "ArticleViewModel" which is responsible of my MvxBindableListView?
Any suggestions?
Your analysis is definitely correct about where the click event is trying to bind.
There are two approaches I generally take:
So...1
The Main Menu in the tutorial has a ViewModel a bit like:
This is used in axml as:
This approach can only be done for ItemClick on the whole list item - not on individual subviews within the list items.
Or...2
Since we don't have any
RelativeSource
binding instructions in mvx, this type of redirection can be done in the ViewModel/Model code.This can be done by presenting a behaviour-enabled wrapper of the Model object rather than the Model object itself - e.g. using a
List<ActiveArticle>
:Your axml would then have to use bindings like:
and
One example of this approach is the Conference sample which uses WithCommand
However... please note that when using
WithCommand<T>
we discovered a memory leak - basically the GarbageCollection refused to collect the embeddedMvxRelayCommand
- which is whyWithCommand<T>
isIDisposable
and why BaseSessionListViewModel clears the list and disposes the WithCommand elements when views are detached.Update after comment:
If your data list is large - and your data is fixed (your articles are models without PropertyChanged) and you don't want to incur the overhead of creating a large
List<WrappedArticle>
then one way around this might be to use aWrappingList<T>
class.This is very similar to the approach taken in Microsoft code - e.g. in virtualizing lists in WP7/Silverlight - http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx
For your articles this might be: