【发布时间】:2019-04-20 15:54:18
【问题描述】:
在我的项目中,我有两个 ListView 在不同的 ViewModels 中具有相同的 ViewCell 内容。我将这个ViewCell 提取到其他XAML 文件中以在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>
...
这可以完美地显示数据,但是在标签中绑定命令时,它不起作用。
<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 命令。我的想法是从它所在的视图绑定两个命令。如何做呢?非常感谢!!
【问题讨论】:
-
你能分享整个 ViewCell 的源代码,包括后面的代码(我猜是
ItemList元素)?看起来BindingContext设置不正确 -
您好!我刚刚编辑了代码以显示所有内容。非常感谢!
-
Humn...您正在使用 MvvmCross,我不知道它是如何工作的,或者它是否会干扰行为,但请查看我的答案,看看它是否适合您。跨度>
标签: listview xamarin xamarin.forms binding