Events 点击项目时,导航到模型上定义的特定页面

Events 点击项目时,导航到模型上定义的特定页面,events,xamarin.forms,model,navigation,Events,Xamarin.forms,Model,Navigation,我正在尝试创建一个自定义主页,其中页面在水平滚动视图中作为“服务”列出,因此每个页面都应该导航到不同的页面 我有这样的看法: <controls:HorizontalScrollView HeightRequest="160" Orientation="Horizontal" ItemsSource="{Binding Ow

我正在尝试创建一个自定义主页,其中页面在水平滚动视图中作为“服务”列出,因此每个页面都应该导航到不同的页面

我有这样的看法:

           <controls:HorizontalScrollView HeightRequest="160" 
                                   Orientation="Horizontal"  
                                   ItemsSource="{Binding OwnerServicesList}"
                                   x:Name="OwnerServicesSlider"
                                   ItemSelected="OwnerServicesSlider_ItemSelected">

                <controls:HorizontalScrollView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="10,0,5,0" WidthRequest="100" HeightRequest="100">
                                <Image HorizontalOptions="Start"  Source="{Binding ImgUrl}" WidthRequest="100" HeightRequest="100" />
                                <Label Style="{StaticResource BoldLabel}" HorizontalTextAlignment="Center" FontSize="13" LineBreakMode="TailTruncation" Text="{Binding Name}" TextColor="Black"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
private void OwnerServicesSlider_ItemSelected(object sender, ItemTappedEventArgs e)
        {
            var service = OwnerServicesSlider.SelectedItem as Services;   
            Navigation.PushAsync(service.Page);
        }
它没有显示错误,但当我运行它时,我得到一个

InvalidOperationException:'页面必须没有父级


任何提示都将不胜感激

正如Jason所说,也许你要推的页面存在于当前导航结构中,在推页面之前,有一个解决方法:

 private void OwnerServicesSlider_ItemSelected(object sender, ItemTappedEventArgs e)
    {
        var service = OwnerServicesSlider.SelectedItem as Services;
        service.Page.Parent = null;             
        Navigation.PushAsync(service.Page);
    }

您正在尝试推送当前导航结构中已存在的页面实例。这是不允许的。请检查您要启动的页面是否已打开。请在pushAsync之前尝试设置service.page.Parent=null。谢谢@LeoZhu MSFT您的awnser工作得很好。我是stackOverflow的新手,我如何才能将您的commnet标记为awnser?@Jd996我已将其作为答案发布,您可以查看