Monodroid将Viemodel命令绑定到MvxGridViewItem元素

时间:2015-12-11 10:58:14

标签: c# gridview binding xamarin.android mvvmcross

我有一个带有他的ItemsSource的MvxGridView到ViewModel中的ObservableCollection

<Mvx.MvxGridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:verticalSpacing="4dp"
            android:horizontalSpacing="4dp"
            android:numColumns="4"
            android:id="@+id/ImageGrid"
            android:background="@android:color/white"
            local:MvxBind="ItemsSource SelectedImages"
            local:MvxItemTemplate="@layout/gridimagelayout" />

显示用户已选择的图像列表。问题出在gridimagelayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="50dp"
    android:layout_height="50dp">
    <View
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="#ff000044"
        local:MvxBind="Visibility Visibility(ValidResolution)"/>
    <Mvx.MvxImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        local:MvxBind="Bitmap Thumbnail" />
    <ImageView
        android:id="@+id/DeleteButton"
        android:layout_height="18dp"
        android:layout_width="18dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="4dp"
        android:background="@drawable/XButton"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

视图和MvxImageView绑定到它们的项目以显示图像,这可以正常工作。但是最后一个元素的目的,即imageview(可能会更改为imagebutton),它应该调用一个Command来删除图像。此命令位于ViewModel中。

现在我的猜测是我应该使用适配器将此按钮绑定到该命令,但我似乎找不到任何好的解决方案。

在Windows Phone中有一种简单的方法可以只改变DataContext,monodroid mvvmcross有这样的方法吗?

1 个答案:

答案 0 :(得分:1)

这就是我的所作所为:

<Mvx.MvxImageView
    android:id="@+id/add_player"
    style="?android:borderlessButtonStyle"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:background="?android:attr/selectableItemBackground"
    local:MvxBind="ImageUrl 'res:icon_add'; Visibility Visibility(RowItem.CanAdd); Click AddPlayerCommand"
    android:contentDescription="text"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter" />

在列表视图模型中:

public IMvxCommand AddPlayerCommand
    {
        get
        {
            return new MvxCommand(() => _parent.ExecuteAddPlayerCommand(new PlayerViewModelWrapper(RowItem, _parent, _listType, _messenger)));
        }
    }

在页面视图模型中:

public override void ExecuteAddPlayerCommand(PlayerViewModelWrapper viewModel)
    {
        _messageService.AddPlayer(viewModel, this);
    }