我有一个带有命令(AddToFavoriteCommand)的ViewModel,但该命令未被调用。现在它只在CustomPin类中查找命令,而不是viewModel。我在代码后台将我的viewModel设置为页面的BindingContext。但由于它枚举了我的customPins集合,因此在那里查找命令。我需要回到根源。我可能需要更改源,但无法使其工作。
<ContentPage.Content>
<StackLayout>
<AbsoluteLayout>
<Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
</AbsoluteLayout>
<ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Favorite" Command="{Binding AddToFavoriteCommand}" />
<MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
</ViewCell.ContextActions>
<StackLayout Padding="5" Orientation="Vertical" >
<Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
代码后端(删除了所有其他逻辑,如我不需要的点击事件)
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ParkingListPage : ContentPage
{
public ParkingListPage()
{
InitializeComponent();
BindingContext = new ParkingListViewModel();
}
}
我的ViewModel
public class ParkingListViewModel
{
public ParkingListViewModel()
{
AddToFavoriteCommand = new Command(Test);
}
private void Test()
{
}
public IEnumerable<CustomPin> CustomPins { get; set; } = SampleParkings.Parkings;
public Command AddToFavoriteCommand { get; }
}