【问题标题】:Navigation PushModalAsync not working inside of event inside ListView导航 PushModalAsync 在 ListView 内的事件内部不起作用
【发布时间】:2020-08-11 20:58:42
【问题描述】:

我有一个列表视图,其中项目模板由几个元素组成,其中一个是添加了TapGestureRecognizer 的框架,如下例所示:

<ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid Margin="10,5" HorizontalOptions="FillAndExpand" RowSpacing="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <!-- Other Element -->

                    <Frame x:Name="SampleFrame" Grid.Column="1"/>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
public TVEntryItem()
{
    InitializeComponent();
            
    var TapGesture = new TapGestureRecognizer();
    TapGesture.Tapped += TapGesture_Tapped;
    SampleFrame.GestureRecognizers.Add(TapGesture);
}

private void TapGesture_Tapped(object sender, EventArgs e)
{
    Navigation.PushModalAsync(new DetailedView());
}

现在,当我点击框架时,确实调用了该事件并创建了 DetailedView 的新实例 - 通过 DetailedView 构造函数中的断点验证 - 但页面本身从未显示,并且中断DetailedView OnAppearing 内的点永远不会被击中。

我在这里错过了什么?!有什么建议么 ?!提前谢谢^^。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    在您的情况下, Frame 位于 ViewCell 中,因此如果您直接在 ContentPage 中处理它,它将永远不会。

    首先,确保应用的MainPageNavigationPage

    在 App.xaml.cs 中

      public App()
        {
            InitializeComponent();
    
            MainPage = new NavigationPage(new xxxPage());
        }
    

    如果你想在框架上添加一个 TapGesture,你可以检查以下代码。

    在 Xaml 中

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 
                 x:Name="Page"  // set the name of contentpage here
                 
                 x:Class="xxx.xxxPage">
    
    <Frame  Grid.Column="1" BackgroundColor="Red">
    
        <Frame.GestureRecognizers>
    
            <TapGestureRecognizer Command="{Binding Source={x:Reference Page}, Path=BindingContext.TapCommand}" />
    
        </Frame.GestureRecognizers>
    
    </Frame>
    

    在内容页面中

     public xxxPage()
        {
            InitializeComponent();
            BindingContext = new xxxViewModel(this.Navigation);
        }
    

    在视图模型中

    公共类 xxxViewModel {

        public ICommand TapCommand { get; private set; }
    
        INavigation Navigation;
    
        public MyViewModel(INavigation navigation)
        {
    
            this.Navigation = navigation;
    
          
    
            TapCommand = new Command(()=> {
    
                Navigation.PushModalAsync(new Page1());
    
            });
    
           // other code ,like setting itemsource of listview
    
        }
    }
    

    【讨论】:

    • 好吧,我上面展示的例子是一个更大的东西的一个非常简单的参考,这使得应用你的方法非常困难,但是使用你所说的我可以想出一个解决方案。
    【解决方案2】:

    感谢Lucas Zhang - MSFT 的回答,我可以想出一个解决方案。正如他所描述的,我无法在ViewCell 中调用导航服务,因此,我只是从主页调用了导航服务。

    private void TapGesture_Tapped(object sender, EventArgs e)
    {
        App.Current.MainPage.Navigation.PushModalAsync(new DetailedView());
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

       Navigation.PushModalAsync(new NavigationPage(new DetailedView()));
      

      【讨论】:

        猜你喜欢
        • 2020-06-23
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多