命令不适用于 MVVM 模式中的资源。我尝试使用 Ancestor 和视图模型 (CustomersViewModel) 设置源,并使用对父内容页面 (CustomersView) 的引用。数据显示正常,问题仅在于未触发的命令。
<CollectionView x:Name="CollectionView" ItemsSource="{Binding Customers}"
ItemTemplate="{StaticResource customerDataTemplateSelector}">
</CollectionView>
在ContentPage.Resources
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="customerTemplate">
<Grid BackgroundColor="{StaticResource NoColor}" Padding="10,5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Text="{Binding Name}" LineBreakMode="WordWrap" FontSize="Medium" LineHeight="1.1"/>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="1"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="75*"/>
</Grid.ColumnDefinitions>
<!-------THIS IS NOT WORKING WITH Ancestor Type--------->
<StackLayout Grid.Column="0" Grid.Row="1" Spacing="2" Padding="0" Orientation="Horizontal">
<Label FontFamily="{StaticResource FontAwesomeSolid}" VerticalOptions="Center" Text="{x:Static fontawesome:FontAwesomeIcons.Share}" Style="{StaticResource LiteDarkLabelStyle}"></Label>
<Label Text="Share" Style="{StaticResource ListItemFooterLabelStyle}"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.ShareCustomerCommand, Source={RelativeSource AncestorType={x:Type local:CustomersViewModel}}}" CommandParameter="{Binding .}" NumberOfTapsRequired="1">
</TapGestureRecognizer>
</StackLayout.GestureRecognizers>
</StackLayout>
<!-------EVEN THIS IS NOT WORKING - setting source as parent content page--------->
<StackLayout Grid.Column="1" Grid.Row="1" Spacing="2" Padding="0" HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label FontFamily="{StaticResource FontAwesomeSolid}" VerticalOptions="Center" Text="{x:Static fontawesome:FontAwesomeIcons.Favorite}" TextColor="{Binding IsFavorite, Converter={Helpers:Highlighter}}"></Label>
<Label Text="Favorite" TextColor="{Binding IsFavorite, Converter={Helpers:Highlighter}}"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.FavoriteCustomerCommand, Source={x:Reference CustomersView}}" CommandParameter="{Binding .}" NumberOfTapsRequired="1">
</TapGestureRecognizer>
</StackLayout.GestureRecognizers>
</StackLayout>
</Grid>
<BoxView Grid.Row="2" HeightRequest="8"
BackgroundColor="{StaticResource LiteColor}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="customerReadModeTemplate">
<StackLayout Padding="10,0" BackgroundColor="{StaticResource NoColor}">
<Label LineBreakMode="WordWrap" LineHeight="1.1">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="{Binding Name}" FontSize="Small"/>
<Span Text=". " />
<Span Text="{Binding City}" FontSize="Medium" />
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</DataTemplate>
<Helpers:CustomerDataTemplateSelector x:Key="customerDataTemplateSelector"
CustomerReadModeTemplate="{StaticResource customerReadModeTemplate}"
CustomerTemplate="{StaticResource customerTemplate}"
/>
</ResourceDictionary>
</ContentPage.Resources>
编辑 另外,我尝试设置相对源
<ContentPage ..
BindingContext="{Binding Source={RelativeSource Self}, Path=ViewModel}">
在页面中
public partial class CustomersPage {
public CustomersPage ()
{
var customersService = new CustomersService();
ViewModel = new CustomersViewModel(customersService);
}
public CustomersViewModel ViewModel { get; set; }
}
另外,我最初也有这个也不起作用。
public CustomersViewModel ViewModel
{
get { return BindingContext as CustomersViewModel; }
set { BindingContext = value; }
}