Listview 如何在Xamarin表单中的可重用ViewCell中绑定命令?
在我的项目中,我有两个Listview 如何在Xamarin表单中的可重用ViewCell中绑定命令?,listview,xamarin,xamarin.forms,binding,Listview,Xamarin,Xamarin.forms,Binding,在我的项目中,我有两个ListView在不同的ViewModels中,它们具有相同的ViewCell内容。我在otherXAML文件中提取这个ViewCell,以便在ListView中重复使用,如下所示: <views:MvxContentPage.Content> <ScrollView x:Name="scrollList"> <StackLayout x:Name="Root"> &
ListView
在不同的ViewModels
中,它们具有相同的ViewCell
内容。我在otherXAML
文件中提取这个ViewCell
,以便在ListView
中重复使用,如下所示:
<views:MvxContentPage.Content>
<ScrollView x:Name="scrollList">
<StackLayout x:Name="Root">
<!-- ... -->
<repeater:RepeaterView x:Name="MainList" ShowSeparator="False"
SelectedItemCommand="{Binding SelectedCommand}"
IsVisible="True"
ItemsSource="{Binding Items}">
<repeater:RepeaterView.ItemTemplate>
<DataTemplate>
<local:ItemList FavoriteCommand="Binding path=FavCommand, Source={x:Reference MainList}}"
FavoriteCommandParameter="{Binding .}"/>
</DataTemplate>
</repeater:RepeaterView.ItemTemplate>
</repeater:RepeaterView>
<!-- ... -->
</StackLayout>
</ScrollView>
</views:MvxContentPage.Content>
...
...
<ListView ...>
<ListView.ItemTemplate>
<DataTemplate>
<local:ItemListCell Name="{Binding Nombre}"
FavoriteCommand="{Binding FavCommand}"
FavoriteCommandParameter="{Binding .}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
...
...
这非常适合显示数据,但当在标签中绑定命令时,它不起作用
<views:MvxViewCell
xmlns="http://xamarin.com/schemas/2014/forms"
x:TypeArguments="viewModels:ItemListViewModel"
xmlns:viewModels="clr-namespace:Template.Core.ViewModels;assembly=Template.Core"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Template.Core.Views.ItemList"
xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:iconize="clr-namespace:Plugin.Iconize;assembly=Plugin.Iconize"
xmlns:Helpers="clr-namespace:Template.Core.Helpers">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Source="ukflag"
Grid.Row="1"
Grid.RowSpan="4"
WidthRequest="50"
HeightRequest="80"
Grid.Column="0" />
<Label Text="{Binding Nombre}"
Grid.Row="1"
Grid.Column="1"
FontAttributes="Bold"
FontSize="15" />
<Label Text="{Binding Direcciones[0].toString}"
Grid.Row="2"
Grid.Column="1"
FontSize="11" />
<iconize:IconLabel Text="fas-heart"
BackgroundColor="Transparent"
Grid.Row="1"
Grid.Column="2"
FontSize="35"
Grid.RowSpan="2"
VerticalOptions="Center">
<iconize:IconLabel.GestureRecognizers>
<TapGestureRecognizer Command="{Binding FavCommand}"
CommandParameter="{Binding .}" />
</iconize:IconLabel.GestureRecognizers>
</iconize:IconLabel>
<StackLayout Orientation="Horizontal"
Grid.Row="3"
Grid.Column="1">
<iconize:IconLabel Text="fas-map-marker-alt"
TextColor="Black"
FontSize="15" />
<Label Text="{Binding Distancia}"
FontSize="13" />
</StackLayout>
</Grid>
</views:MvxViewCell>
public partial class ItemList
{
public ItemList()
{
InitializeComponent();
}
}
公共部分类项目列表
{
公共项目列表()
{
初始化组件();
}
}
在分离文件中的ViewCell之前,绑定工作正常,但现在不调用ViewModel命令。我的想法是从它所在的视图绑定两个命令。怎样才能做到呢?非常感谢 我不知道您对Xamarin表单的熟练程度如何,但它肯定不是初学者的代码。我想这只是一个误会。正如我所评论的,您描述的问题看起来像是一个绑定问题 因此,蛋糕的配方是:
// viewCell's code-behind
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemListCell : ContentView
{
public static BindableProperty NameProperty = BindableProperty.Create(nameof(Name), typeof(string), typeof(ItemListCell));
public string Name
{
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
public static BindableProperty FavoriteCommandProperty = BindableProperty.Create(nameof(FavoriteCommand), typeof(ICommand), typeof(ItemListCell));
public ICommand FavoriteCommand
{
get => (ICommand)GetValue(FavoriteCommandProperty);
set => SetValue(FavoriteCommandProperty, value);
}
public static BindableProperty FavoriteCommandParameterProperty = BindableProperty.Create(nameof(FavoriteCommandParameter), typeof(object), typeof(ItemListCell));
public object FavoriteCommandParameter
{
get => GetValue(FavoriteCommandParameterProperty);
set => SetValue(FavoriteCommandParameterProperty, value);
}
public static BindableProperty DistanceProperty = BindableProperty.Create(nameof(Distance), typeof(string), typeof(ItemListCell));
public string Distance
{
get => (string)GetValue(DistanceProperty);
set => SetValue(DistanceProperty, value);
}
public ItemListCell ()
{
InitializeComponent ();
}
}
2-在[view cell]内部绑定上使用属性
在ViewCell xaml上,应该将内部元素属性绑定到已知的绑定上下文(在本例中,我将其命名为this
)。请参见标签
和标签识别器
上的示例:
// viewCell's XAML
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App2.ItemListCell"
x:Name="This">
<ContentView.Content>
<Grid BindingContext="{x:Reference This}">
<!-- Keep all the viewcell xaml source code unmodified -->
<Label Text="{Binding Name}"/>
<!-- ... -->
<iconize:IconLabel.GestureRecognizers>
<TapGestureRecognizer Command="{Binding FavoriteCommand}"
CommandParameter="{Binding FavoriteCommandParameter}" />
</iconize:IconLabel.GestureRecognizers>
<!-- ... -->
<!-- Keep all the viewcell xaml source code unmodified -->
</Grid>
</ContentView.Content>
</ContentView>
更新:如果您在页面的视图模型上使用FavCommand的单个实现:
...
...
我希望它能有所帮助。你能分享整个ViewCell的源代码,包括隐藏的代码(我猜是
ItemList
元素)吗?看起来,BindingContext
未正确设置Hello!我只是编辑代码来显示所有内容。非常感谢你!哼。。。您正在使用MvvmCross,我不知道它是如何工作的,或者它是否会干扰行为,但是请查看我的答案,看看它是否对您有效。您好,我在Listview中遇到问题。我把它改成了RepaterView。我不能把我最喜欢的东西绑起来。我已经调试过并且从未调用set方法,只有get方法。我已经按照您的代码说明进行了操作,包括我已经使用Listview进行了测试,但它仍然不起作用。会发生什么?谢谢对不起,我不知道RepeaterView的事。要绑定的FavCommand
是在ItemViewModel
或PageViewModel
上实现的命令?我想这是第二个。在这种情况下,您必须对客户端上的命令bind进行交叉绑定。查看我的更新。@pablogupiListView
有什么问题?
<ContentPage ...
x:Name="PageInstance">
...
<ListView ...>
<ListView.ItemTemplate>
<DataTemplate>
<local:ItemListCell Name="{Binding Nombre}"
FavoriteCommand="{Binding BindingContext.FavCommand, Source={x:Reference PageInstance}}"
FavoriteCommandParameter="{Binding .}"
.../>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
...
</ContentPage>