Monodroid 将 Viewmodel 命令绑定到 Mvc GridView Item 元素
Monodroid bind Viemodel command to MvxGridViewItem element
我有一个 MvxGridView,他的 ItemsSource 指向 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)的目的是它应该调用命令来删除图像。这个命令在 ViewModel 中。
现在我的猜测是我应该使用适配器将此按钮绑定到该命令,但我似乎找不到任何好的解决方案。
在WindowsPhone中有一个简单的方法来改变DataContext,monodroid mvvmcross有这样的方法吗?
我就是这样做的:
<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);
}
我有一个 MvxGridView,他的 ItemsSource 指向 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)的目的是它应该调用命令来删除图像。这个命令在 ViewModel 中。
现在我的猜测是我应该使用适配器将此按钮绑定到该命令,但我似乎找不到任何好的解决方案。
在WindowsPhone中有一个简单的方法来改变DataContext,monodroid mvvmcross有这样的方法吗?
我就是这样做的:
<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);
}