使用绑定从 CollectionView 的项模板调用命令时不触发



我有麻烦导航从ViewModel使用绑定,命令async方法onmovieclick根本不触发。

public class MainViewModel : BaseViewModel
{
private List<Movie> popularMovies;
public List<Movie> PopularMovies
{
get => popularMovies;
set => SetProperty(ref popularMovies, value);
}
public ICommand NavigateToMovieCommand { get; set; }
private readonly TMDBService tmdbService;
private readonly INavigationService navigationService;
public MainViewModel()
{
tmdbService = new TMDBService();
navigationService = new NavigationService();
NavigateToMovieCommand = new Command<int>(async (id) => await OnMovieClicked(id));
InitAsync();
}
private async void InitAsync()
{
PopularMovies = await tmdbService.GetPopularMovies();
}
private async Task OnMovieClicked(int id)
{
Console.WriteLine("****Navigate");
var viewModel = new MovieViewModel(id);
await navigationService.PushAsync(new MoviePage(viewModel));
}

它没有写任何东西到控制台,我已经测试了我的导航服务,它工作正常

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:MovieApp.ViewModels"
Title="New Releases"
x:Class="MovieApp.MainPage"
x:Name="MoviePage"
BackgroundColor="DarkSlateGray">
<ContentPage.BindingContext>
<viewmodels:MainViewModel/>
</ContentPage.BindingContext>
<CollectionView ItemsSource="{Binding PopularMovies}"
SelectionMode="None"
ItemSizingStrategy="MeasureAllItems"
ItemsLayout="VerticalGrid, 3">
<CollectionView.ItemTemplate>
<DataTemplate>
<ContentView>
<Frame HasShadow="True"
Margin="10"
WidthRequest="80"
BackgroundColor="LightSlateGray"
Padding="0"
CornerRadius="15">
<Grid RowDefinitions="120,25">
<Image Aspect="AspectFill"
Source="{Binding PosterPath}"
Grid.RowSpan="2"/>
<BoxView WidthRequest="80"
Grid.Row="1"
Opacity="0.85"
BackgroundColor="LightSlateGray"/>
<Label Grid.Row="1"
TextColor="White"
HorizontalOptions="CenterAndExpand"
Padding="8,0"
Text="{Binding Title}"
LineBreakMode="TailTruncation"/>
<Button Grid.RowSpan="2"
BackgroundColor="Blue"
Command="{Binding BindingContext.NavigateToMovieCommand, Source={x:Reference MoviePage}}"
CommandParameter="{Binding Id}"/>
</Grid>
</Frame>
</ContentView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>

我已经添加了x:Name和x:Reference属性到Page和Command

我不认为我在XAML文件中做错了什么,但它必须是一个绑定问题,因为它不将日志行打印到控制台。

更新:

这是Movie模型

public partial class Movie
{ 
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("poster_path")]
public string PosterPath {
get;
set;
}
[JsonProperty("release_date")]
public string ReleaseDate { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("vote_average")]
public double VoteAverage { get; set; }
[JsonProperty("credits", NullValueHandling = NullValueHandling.Ignore)]
public Credits Credits { get; set; }
}

您的代码是可以的,没有问题,但是您没有发布一些关于Movie的代码,Id property类型是intstring,如果MovieId propertystring,在NavigateToMovieCommand中传递Id参数为int类型,则命令不会触发。

NavigateToMovieCommand = new Command<int>(async (id) => await OnMovieClicked(id));

我使用以下Movie类,将Id属性类型设置为int,并使用您的代码进行测试,可以触发按钮命令,没有问题。

public class Movie
{
public int Id { get; set; }
public string PosterPath { get; set; }
public string Title { get; set; }
}

最新更新