【问题标题】:Xamarin.Forms bindingContext Set the source back to root/parentXamarin.Forms bindingContext 将源设置回根/父
【发布时间】:2017-12-15 14:33:41
【问题描述】:

我得到了一个 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; }
}

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    试试这样:

    <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 绑定中引用该名称并添加Path=BindingContext.。这样,它将绑定到由名称标识的元素的BindingContext,在我们的例子中是ContentPage

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

    【讨论】:

    • 按你说的那样工作。现在它正在调用后面的代码。但我希望它调用我的 viewModel。现在在我后面的代码中,我只像这样设置我的 BindingContext,BindingContext = new ViewModel();我应该为我的 viewModel 创建一个属性,以便我可以使用路径或其他东西吗?
    • 这应该适用于视图模型。请使用有关如何设置视图模型的代码更新您的问题。
    • 我更新了我的代码。我删除了所有其他方法,例如点击事件。如果重要的话,我也添加了我的 viewModel。
    • 使用我发布的 XAML,这应该可以工作。在你的测试方法中实现一些逻辑,它应该被调用。
    • 我实际上忘记了 MenuItem 绑定中的一个小而重要的细节,我已经更新了答案并添加了一个示例项目。干杯!
    猜你喜欢
    • 2016-12-08
    • 2016-11-27
    • 2019-10-10
    • 2020-06-29
    • 2015-02-09
    • 2019-04-24
    • 2017-07-07
    • 1970-01-01
    相关资源
    最近更新 更多