命令 RelativeSource 在 Xamarin 中不起作用

Command RelativeSource not working in Xamarin

我在子页面中有绑定数据问题。我有一个显示产品列表的页面。单击产品时,它将转到“产品详细信息”子页面。一切正常:

Product.xaml

<RefreshView x:DataType="locals:DashboardViewModel" Padding="0" Command="{Binding LoadDashboardCommand}" IsRefreshing="{Binding IsBusy, Mode=OneWay}">
    <StackLayout BindableLayout.ItemsSource="{Binding ProductNew}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Frame x:DataType="model:Product">
                    <StackLayout>
                        <Label FontSize="14" MaxLines="2" LineBreakMode="TailTruncation" HeightRequest="40" Text="{Binding Name}"/>
                    </StackLayout>
                    <Frame.GestureRecognizers>
                        <TapGestureRecognizer NumberOfTapsRequired="1" 
                            Command="{Binding Source={RelativeSource AncestorType={x:Type locals:DashboardViewModel}}, Path=ProductTappedView}" 
                            CommandParameter="{Binding .}" />
                    </Frame.GestureRecognizers>
                </Frame>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</RefreshView>

Product.xaml.cs

DashboardViewModel dashboardViewModel;
public Product()
{
    BindingContext = dashboardViewModel = new DashboardViewModel(Navigation);
    dashboardViewModel.OnAppearing();
}

DashboardViewModel.cs

public class DashboardViewModel : BaseDashboardViewModel
{
    public Command LoadDashboardCommand { get; }
    public Command ProductTappedView { get; }
    public ObservableCollection<Product> ProductNew { get; }
    public DashboardViewModel(INavigation _navigation)
    {

        LoadDashboardCommand = new Command(async () => await ExecuteLoadDashboardCommand());
        ProductNew = new ObservableCollection<Product>();
        ProductTappedView = new Command<Product>(OnViewDetailProduct);
        Navigation = _navigation;
    }
    private async void OnViewDetailProduct(Product detailProduct)
    {
        await Navigation.PushAsync(new ProductDetail(detailProduct));
    }
    ............
}

接下来在我的Productdetail页面显示产品详情。我有阅读更多。单击后将重定向到另一个子页面:ContentDetailProd.xaml

ProductDetail.xaml

<ContentPage.BindingContext>
    <locals:ViewDetailProductViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout Padding="15">
        <Label Text="{Binding ProductNews.Name}" FontFamily="RobotoMedium" FontSize="18" TextTransform="Uppercase"/>
        <Label Text="Read more"/>
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding Source={RelativeSource AncestorType={x:Type locals:ViewDetailProductViewModel}}, Path=ContentProductTappedView}" CommandParameter="{Binding .}" />
        </StackLayout.GestureRecognizers>
    </StackLayout>
</ContentPage.Content>

ViewDetailProductViewModel.cs

public class ViewDetailProductViewModel : BaseDashboardViewModel
{
    public Command ContentProductTappedView { get;}
    public ViewDetailProductViewModel()
    {
        ProductNews = new Product();
        ContentProductTappedView = new Command<Product>(OnViewContentDetailProduct);
    }
    private async void OnViewContentDetailProduct(Product detailProduct)
    {
        await Navigation.PushAsync(new ContentDetailProd(detailProduct));
    }
}

ContentDetailProd.xaml.cs

public ContentDetailProd(Product detailProduct)
{
    InitializeComponent();
    //Load Readmore
}

然而,当我调试时它实际上并没有 运行 事件:ContentProductTappedView 因此它没有重定向到 ContentDetailProd.xaml 页面.我什么都试过了还是不能解决问题

期待大家的帮助。谢谢

如果想通过Page Constructor传递数据,可以参考下面的link。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical#passing-data-when-navigating

我根据@Wendy Zang - MSFT 提供的文档进行了这样的更改。一切看起来都很好。

ProductDetail.xaml

<StackLayout x:Name="_readmore" Padding="15">
    <Label Text="{Binding ProductNews.Name}" FontFamily="RobotoMedium" FontSize="18" TextTransform="Uppercase"/>
    <Label x:Name="_content" MaxLines="2" LineBreakMode="TailTruncation" Text="{Binding ProductNews.Contents}" FontFamily="RobotoMedium" FontSize="18" TextTransform="Uppercase"/>
    <Label Text="Read more"/>
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Tapped="_readmore_Tapped" />
    </StackLayout.GestureRecognizers>
</StackLayout>

ProductDetail.xaml.cs

private async void _readmore_Tapped(object sender, EventArgs e)
{
    var contentget = _content.Text;      

    var detailProduct = new Product
    {
        Contents = contentget,
    };          

    var detailContentPage = new ContentDetailProd(detailProduct);
    detailContentPage.BindingContext = detailProduct;
    await Navigation.PushAsync(detailContentPage);
}

ContentDetailProd.xaml.cs

public ContentDetailProd(Product detailProduct)
{
    InitializeComponent();
    //Load Readmore
    
    if (detailProduct != null)
    {
        _contentmore.Text = detailProduct.Contents;
    }
}