绑定到ListView项挖掘财产视图模型 [英] Binding to ListView item tapped property from View Model
问题描述
我想一个事件绑定到ListView,我的菜单页中,使用itemtapped财产。目前我使用MVVM(Xamarin形式实验室)框架在我的应用程序。当用户点击应用程序导航到正确的观点菜单项是什么,我试图完成的。
下面是XAML代码:
< ListView的X: NAME =列表
的ItemsSource ={结合的MenuItems}
的SelectedItem ={结合的SelectedItem}
ItemTapped = SET结合-HERE>
< ListView.ItemTemplate>
<&DataTemplate的GT;
< ViewCell>
< ViewCell.View>这里
//设置模板
< /ViewCell.View>
< / ViewCell>
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;
我要的itemtapped事件给这个函数绑定:
<预类=郎-CS prettyprint-覆盖>
公共无效的NavigateTo(对象发件人,ItemTappedEventArgs参数)
{
VAR测试= args.Item为MenuModel;
cPageTypes.GetByKey(test.CommandParameter)
.SwitchRootPage(AIMCore.ViewModels.ElliottBaseViewModel.MasterPage);
List.selectedItem单选= NULL;
AIMCore.ViewModels.BaseViewModel.MasterPage.IsPresented = FALSE;
}
目前,我能得到这个工作,如果我添加了功能视图的代码背后,然后将 ItemTapped ='NavigatTo ,但因为它违背了MVVM的概念,这似乎是错误的。我真正想要做的是绑定事件此相同的功能在我的视图模型是这样的:
< ListView的X:名称=列表
的ItemsSource ={结合的MenuItems}
的SelectedItem ={结合的SelectedItem}
ItemTapped ={结合的NavigateTo}> //这是绑定到视图模型
然而,这不工作或我没有做正确。当我尝试实现这种方式的代码生成和误差
错误:
Xamarin.Forms.Xaml.XamlParseException:否名字的酒店在Xamarin.Forms.Xaml.BaseValueNode.SetPropertyValue ItemTapped找到
我已经遵循了相同的架构,并通过完成创建自定义列表控件并创造1绑定属性与我用下面的代码在我的视图模型已经覆盖命令:
自定义控制[的.cs]在我的PCL页
使用系统;
使用System.Windows.Input;使用Xamarin.Forms
;
命名空间YourNS {
公共类的ListView:Xamarin.Forms.ListView {
公共静态BindableProperty ItemClickCommandProperty = BindableProperty.Create< ListView控件,ICommand的>(X => x.ItemClickCommand,NULL);
公众的ListView(){
this.ItemTapped + = this.OnItemTapped;
}
公众的ICommand ItemClickCommand {
{返回(的ICommand)this.GetValue(ItemClickCommandProperty); }
集合{this.SetValue(ItemClickCommandProperty,值); }
}
私人无效OnItemTapped(对象发件人,ItemTappedEventArgs E){
如果(e.Item = NULL&放大器;!&安培;!this.ItemClickCommand =空&功放;&安培; this.ItemClickCommand.CanExecute(E)){
this.ItemClickCommand.Execute(e.Item);
this.SelectedItem = NULL;
}
}
}
}
我的XAML页
< ContentPage ...
的xmlns:地方=CLR的命名空间:Samples.Views;装配=您的Assebly名称>
<局部:ListView控件ItemClickCommand ={绑定选择}
的ItemsSource ={绑定列表}>
和我的视图模型[在这个例子中,我只打开的对话框动作片
私人命令<签署及GT;设置SelectCmd;
公共命令<签署及GT;选择{
获得{
this.selectCmd = this.selectCmd?新的命令<签名>(S =>
this.dialogs.ActionSheet(新ActionSheetConfig()
。新增(查看,()=>!{
如果(这一点。 fileViewer.Open(s.FilePath))
this.dialogs.Alert(的String.Format(无法打开文件{0},s.FileName));
})
。添加(取消)
)
);
返回this.selectCmd;
}
}
I am trying to bind an event to a ListView, on my menu page, using the itemtapped property. Currently I am using MVVM (Xamarin form labs) framework in my app. What I am trying to accomplish is when a user taps the menu item the app navigates to the correct view.
Here is the xaml code:
<ListView x:Name="list"
ItemsSource="{Binding MenuItems}"
SelectedItem="{Binding SelectedItem}"
ItemTapped= SET-BINDING-HERE >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
//setup template here
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I want to bind the itemtapped event to this function:
public void NavigateTo(object sender, ItemTappedEventArgs args)
{
var test = args.Item as MenuModel;
cPageTypes.GetByKey(test.CommandParameter)
.SwitchRootPage(AIMCore.ViewModels.ElliottBaseViewModel.MasterPage);
list.SelectedItem = null;
AIMCore.ViewModels.BaseViewModel.MasterPage.IsPresented = false;
}
I can currently get this to work if I add the function to the view's code behind and then set the ItemTapped='NavigatTo', but this seems wrong as it defeats the MVVM concept. What I really want to do is bind the event this same functionality in my ViewModel something like this:
<ListView x:Name="list"
ItemsSource="{Binding MenuItems}"
SelectedItem="{Binding SelectedItem}"
ItemTapped= "{Binding NavigateTo}" > // this binding is to the ViewModel
However this is not working or I am not doing it correctly. When I try implement it this way the code produces and error.
Error: Xamarin.Forms.Xaml.XamlParseException: No Property of name ItemTapped found at Xamarin.Forms.Xaml.BaseValueNode.SetPropertyValue
I've followed the same architecture and done through creating custom list control and created 1 bindable property with command which I've override in my View Model using below code:
Custom Control [.cs] page in my PCL
using System;
using System.Windows.Input;
using Xamarin.Forms;
namespace YourNS {
public class ListView : Xamarin.Forms.ListView {
public static BindableProperty ItemClickCommandProperty = BindableProperty.Create<ListView, ICommand>(x => x.ItemClickCommand, null);
public ListView() {
this.ItemTapped += this.OnItemTapped;
}
public ICommand ItemClickCommand {
get { return (ICommand)this.GetValue(ItemClickCommandProperty); }
set { this.SetValue(ItemClickCommandProperty, value); }
}
private void OnItemTapped(object sender, ItemTappedEventArgs e) {
if (e.Item != null && this.ItemClickCommand != null && this.ItemClickCommand.CanExecute(e)) {
this.ItemClickCommand.Execute(e.Item);
this.SelectedItem = null;
}
}
}
}
My XAML Page
<ContentPage ...
xmlns:local="clr-namespace:Samples.Views;assembly=Your Assebly Name">
<local:ListView ItemClickCommand="{Binding Select}"
ItemsSource="{Binding List}">
And in my View Model [In this example, I've only opened dialog action sheet
private Command<Signature> selectCmd;
public Command<Signature> Select {
get {
this.selectCmd = this.selectCmd ?? new Command<Signature>(s =>
this.dialogs.ActionSheet(new ActionSheetConfig()
.Add("View", () => {
if (!this.fileViewer.Open(s.FilePath))
this.dialogs.Alert(String.Format("Could not open file {0}", s.FileName));
})
.Add("Cancel")
)
);
return this.selectCmd;
}
}
这篇关于绑定到ListView项挖掘财产视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!