Xamarin.forms Xamarin表单:如何处理ViewModel中的Listview图像单击事件?
全部, 我正在将Listview绑定到Viewmodel中的集合。ListView的CellView包含一个图像。我想在单击列表项中的图像时调用viewmodel中的命令。我试图避免在模型中处理事件。有什么想法吗 谢谢 下面给出的是xaml和视图模型Xamarin.forms Xamarin表单:如何处理ViewModel中的Listview图像单击事件?,xamarin.forms,Xamarin.forms,全部, 我正在将Listview绑定到Viewmodel中的集合。ListView的CellView包含一个图像。我想在单击列表项中的图像时调用viewmodel中的命令。我试图避免在模型中处理事件。有什么想法吗 谢谢 下面给出的是xaml和视图模型 ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:jList="clr-namespace:JList;assembly=JList"
x:Class="JList.Pages.ItemDetailPage"
Title="Sub Items"
BindingContext="{Binding Source={StaticResource Locator}, Path=ItemDetailViewModel}"
>
<ContentPage.ToolbarItems >
<ToolbarItem Text="Add" Order="Primary" Priority="1" Command="{Binding AddItemCommand}"></ToolbarItem>
<ToolbarItem Text="Edit" Order="Primary" Priority="2" Command="{Binding EditItemCommand}"></ToolbarItem>
</ContentPage.ToolbarItems>
<StackLayout>
<SearchBar Placeholder="Search..." VerticalOptions="Fill" SearchCommand="{Binding SearchCommand}" Text="{Binding SearchString}" ></SearchBar>
<ListView RowHeight="200" ItemsSource="{Binding SubItemsCollection}" BackgroundColor="Gainsboro" SelectedItem="{Binding SubItemSelected, Mode=TwoWay}" x:Name="List" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout>
<Grid VerticalOptions="Fill" BackgroundColor="White" Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="{Binding ImagePath}" Aspect="AspectFit">
<Label Grid.Row="0" Grid.Column="0" Text="{Binding Name}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Chocolate" Font="Bold,20" />
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<ContentPage.Behaviors>
<jList:CustomBehavior />
</ContentPage.Behaviors>
ContentPage xmlns=”http://xamarin.com/schemas/2014/forms"
xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:jList=“clr命名空间:jList;assembly=jList”
x:Class=“JList.Pages.ItemDetailPage”
Title=“子项”
BindingContext=“{Binding Source={StaticResource Locator},Path=ItemDetailViewModel}”
>
视图模型
namespace JList.Core.ViewModels
{
public class ItemDetailViewModel : ViewModelBase, IViewModel
{
private IItemService _itemService;
private ICommandFactory _cmdFactory;
private INavigationService _navService;
private ItemListViewModel _parent;
private IAppInstanceData _appData;
public ItemDetailViewModel(IItemService itemService, ICommandFactory cmdFactory, INavigationService navService, IAppInstanceData appData, ItemListViewModel parent)
{
_itemService = itemService;
_cmdFactory = cmdFactory;
_navService = navService;
_parent = parent;
ParentItemSelected = _parent.ItemSelected.Id;
_appData = appData;
// FetchSubItemsAsync();
}
public int ParentItemSelected { get; set; }
private string _searchString;
public String SearchString
{
get { return _searchString; }
set
{
if (_searchString != value)
{
_searchString = value;
OnPropertyChanged();
}
}
}
private ObservableCollection<SubItem> _subItemsCollection;
public ObservableCollection<SubItem> SubItemsCollection
{
get { return _subItemsCollection; }
set
{
if (_subItemsCollection != null)
{
if (!_subItemsCollection.SequenceEqual(value))
{
_subItemsCollection = value;
OnPropertyChanged();
}
}
else
{
_subItemsCollection = value;
OnPropertyChanged();
}
}
}
private async void FetchSubItemsAsync()
{
ParentItemSelected = _parent.ItemSelected.Id;
var items = await _itemService.GetAllSubItemsAsync(_parent.ItemSelected.Id);
var coll = new ObservableCollection<SubItem>();
foreach (var it in items)
{
coll.Add(it);
}
SubItemsCollection = coll;
}
public void RefreshAsync()
{
FetchSubItemsAsync();
}
private SubItem _itemSelected;
public SubItem SubItemSelected
{
get => _itemSelected;
set
{
_itemSelected = value;
// _navService.PushView(typeof(EditSubItemViewModel).ToString());
}
}
#region FetchCommand
private ICommand _fetchItemsCommand;
public ICommand FetchItemsCommand
{
get
{
if (_fetchItemsCommand == null)
_fetchItemsCommand = _cmdFactory.CreateCommand(FetchSubItemsAsync, () => true);
return _fetchItemsCommand;
}
}
#endregion
#region AddItemCommand
private ICommand _addItemCommand;
public ICommand AddItemCommand
{
get
{
if (_addItemCommand == null)
_addItemCommand = _cmdFactory.CreateCommand(AddItem, () => true);
return _addItemCommand;
}
}
public void AddItem()
{
_appData.IsEditSubItem = false;
_navService.PushView(typeof(SubItemViewModel).ToString());
}
#endregion
#region EditItemCommand
private ICommand _editItemCommand;
public ICommand EditItemCommand
{
get
{
if (_editItemCommand == null)
_editItemCommand = _cmdFactory.CreateCommand(EditItem, () => true);
return _editItemCommand;
}
}
public void EditItem()
{
_appData.IsEditSubItem = true;
_navService.PushView(typeof(SubItemViewModel).ToString());
}
#endregion
#region SearchCommand
private ICommand _searchCommand;
public ICommand SearchCommand
{
get
{
if (_searchCommand == null)
_searchCommand = _cmdFactory.CreateCommand(SearchItemAsync, () => true);
return _searchCommand;
}
}
private async void SearchItemAsync()
{
var items = await _itemService.GetAllSubItemsAsync(_parent.ItemSelected.Id);
var sstring = SearchString.ToLower();
items = items.Where(i => i.Name.ToLower().Contains(sstring));
var coll = new ObservableCollection<SubItem>();
foreach (var it in items)
{
coll.Add(it);
}
SubItemsCollection = coll;
}
#endregion
}
}
namespace JList.Core.ViewModels
{
公共类ItemDetailViewModel:ViewModelBase、IViewModel
{
私人项目服务(itemService);;
私人ICommand工厂(CMDU工厂);
私人INavigationService(导航服务);
private ItemListViewModel\u父项;
私有IAppInstanceData_appData;
public ItemDetailViewModel(IIItemService itemService、ICommandFactory cmdFactory、INavigationService navService、IAppInstanceData appData、ItemListViewModel父级)
{
_itemService=itemService;
_cmdFactory=cmdFactory;
_导航服务=导航服务;
_父母=父母;
ParentItemSelected=\u parent.ItemSelected.Id;
_appData=appData;
//FetchSubItemsAsync();
}
public int ParentItemSelected{get;set;}
私有字符串_searchString;
公共字符串搜索字符串
{
获取{return\u searchString;}
设置
{
if(_searchString!=值)
{
_searchString=值;
OnPropertyChanged();
}
}
}
私有可观测集合\u子项集合;
公共可观测集合子项集合
{
获取{return\u subItemsCollection;}
设置
{
if(_subItemsCollection!=null)
{
如果(!\u子项集合.序列相等(值))
{
_子项集合=值;
OnPropertyChanged();
}
}
其他的
{
_子项集合=值;
OnPropertyChanged();
}
}
}
私有异步void FetchSubItemsAsync()
{
ParentItemSelected=\u parent.ItemSelected.Id;
var items=wait _itemService.GetAllSubItemsAsync(_parent.ItemSelected.Id);
var coll=新的ObservableCollection();
foreach(项目中的var it)
{
coll.Add(it);
}
子项集合=coll;
}
公共void RefreshAsync()
{
FetchSubItemsAsync();
}
私有子项_itemSelected;
公共子项子项已选定
{
get=>\u itemSelected;
设置
{
_itemSelected=值;
//_navService.PushView(typeof(EditSubItemViewModel.ToString());
}
}
#区域获取命令
私有ICommand_fetchItemsCommand;
公共ICommand获取项目命令
{
得到
{
if(_fetchItemsCommand==null)
_fetchItemsCommand=\u cmdFactory.CreateCommand(FetchSubItemsAsync,()=>true);
返回_fetchItemsCommand;
}
}
#端区
#区域AddItemCommand
专用ICommand_addItemCommand;
公共ICommand AddItemCommand
{
得到
{
如果(_addItemCommand==null)
_addItemCommand=\u cmdFactory.CreateCommand(AddItem,()=>true);
返回_addItemCommand;
}
}
公共无效附加项()
{
_appData.IsEditSubItem=false;
_PushView(typeof(SubItemViewModel.ToString());
}
#端区
#区域编辑项命令
私有ICommand_editItemCommand;
公共ICommand editItem命令
{
得到
{
如果(_editItemCommand==null)
_editItemCommand=\u cmdFactory.CreateCommand(EditItem,()=>true);
返回editItemCommand;
}
}
公共无效编辑项()
{
_appData.IsEditSubItem=true;
_PushView(typeof(SubItemViewModel.ToString());
}
#端区
#区域搜索命令
专用ICommandu searchCommand;
公共ICommand SearchCommand
{
得到
{
如果(_searchCommand==null)
_searchCommand=\u cmdFactory.CreateCommand(SearchItemAsync,()=>true);
返回搜索命令;
}
}
私有异步void SearchItemAsync()
{
var items=wait _itemService.GetAllSubItemsAsync(_parent.ItemSelected.Id);
var sstring=SearchString.ToLower();
items=items.Where(i=>i.Name.ToLower().Contains(sstring));
var coll=新的ObservableCollection();
前面
<ContentPage x:Name="ABCPage">
...
<Image Source="abc">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Path=BindingContext.ImageCommand, Source={x:Reference Name=ABCPage}}"
CommandParameter="{Binding .}" />
</Image.GestureRecognizers>
</Image>
...
</ContentPage>