我得到了一个带有未被调用的命令(ViewModel)的AddToFavoriteCommand。现在,它仅负责CustomPin class中的命令,而不是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; }
}

最佳答案

像这样尝试:

<ContentPage x:Name="YourPageName">
    <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 Path=BindingContext.AddToFavoriteCommand, Source={x:Reference YourPageName}}" />
                                <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>
</ContentPage>

请注意,我是如何将x:Name元素添加到页面根目录的。当然,那里还有更多属性,请将它们保留在那里,但在此我为清楚起见将其省略。

其次,请注意我是如何从MenuItem绑定(bind)中引用该名称并添加Path=BindingContext.的。这样,它将绑定(bind)到由名称标识的元素的BindingContext,在本例中为ContentPage

可以在此处找到示例项目:https://github.com/jfversluis/SampleParentBinding

10-08 02:46