简体   繁体   中英

Monodroid bind Viemodel command to MvxGridViewItem element

I have a MvxGridView with his ItemsSource to a ObservableCollection in the ViewModel

<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" />

This displays a list of images that have been selected by the user. The problem is in the 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>

The view and MvxImageView are binded to the their item to display the image, which works fine. But the purpose of the last element, the imageview (will probably be changed to imagebutton), is that it should call a Command to delete the image. And this command is in the ViewModel.

Now my guess is that I should use a adapter to bind this button to that command, but I can't seem to find any good solution for this.

In Windows Phone there is a simple way to just change the DataContext, is there such a way with monodroid mvvmcross?

This is what I do:

<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" />

In list view model:

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

In page view model:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM