the "tapped" method of TapGestureRecognizer can not be fired in contentView in UWP project

Viewed 212

I have a Legacy project of xamarin.form. It can work normally in win10 2018 version. But after the windows update to the lastest version, the UWP exe file can't install in windows system. I'm trying to repaire this project. Now I'm using VS2019, the Xamarin.Forms is lastest(5.0.0.2083), Microsoft.netCore.Platforms is 5.0.2. The solution can be build with some warings, and can run directly on local win10 in debug model.

The app is make up with a Main page frame, and in frame there are several contentView. Now I met a issue that the TapGestureRecognizer didn't work in contentView. But it can work normally in MainPage. Here is the ContentView FirstStartView.xaml:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:localization="clr-namespace:DemoTest.Localization;assembly=DemoTest"
             x:Class="DemoTest.Views.FirstStartView">
    <ContentView.Content>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="{StaticResource DefaultViewMargins}">
            <Label ...... ></Label>
            <StackLayout ......>
                <TableView HeightRequest="220">
                    <TableRoot>
                        <TableSection x:Name="Brand">
                            <SwitchCell AutomationId="aaa" Text="{localization:Translate aaa}" OnChanged="SwitchCell_OnOnChanged"></SwitchCell>
                        </TableSection>
                    </TableRoot>
                </TableView>
            </StackLayout>
            <StackLayout x:Name="ContinueBtn" Orientation="Horizontal" VerticalOptions="EndAndExpand" HorizontalOptions="CenterAndExpand">
                <Label Text="{localization:Translate StartStartSetup}" BackgroundColor="Transparent" " FontSize="{DynamicResource ContinueBtnSize}" VerticalOptions="Center"></Label>
                <Image Source="Assets/RightArrow.png" WidthRequest="40" HeightRequest="40" VerticalOptions="Center"></Image>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="ContinueBtn_OnClicked" NumberOfTapsRequired="1"></TapGestureRecognizer>
                </StackLayout.GestureRecognizers>
            </StackLayout>
        </StackLayout>
    </ContentView.Content>
</ContentView>

the cs file:

namespace DemoTest.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class FirstStartView : ContentView
    {
        .......
        
        private void SwitchCell_OnOnChanged(object sender, ToggledEventArgs e)
        {
            ......
        }

        private async void ContinueBtn_OnClicked(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("--===Debug===--: In FirstStartView: ContinueBtn_OnClicked Entry!");
        }
    }
}

In .cs file the method ContinueBtn_OnClicked will not be call at all. there is nothing happened when click the StackLayout.

But I notice that in MainPageView.xaml the TapGestureRecognizer of frame can work normally:

            <Frame Grid.Column="1" Padding="0" OutlineColor="Black" x:Name="TestSetupPage" AutomationId="TestSetupPage">
                <Label Text="{localization:Translate TestSetupPageLabel}" Style="{StaticResource NavigationPanelStyle}"></Label>
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Button_OnViewSelected"></TapGestureRecognizer>
                </Frame.GestureRecognizers>
            </Frame>

In this page, the "Button_OnViewSelected" method can be invoked. The MainPage is a ContentPage but the FirstStartView is a ContentView if it result the different. Also I notice that in ContentView, all the component can't fired related method. In my post code, the SwitchCell can't run OnChanged(), other label can't run OnClicked() and so on. There is no error when click label or change switchCell or TapGestureRecognizer, just nothing happened.

I doubt if some binding path is broken between xaml and cs; but I'm not good at Xamarin.Form. So I ask here for some suggest.

Thank you all.

1 Answers

the “tapped” method of TapGestureRecognizer can not be fired in contentView in UWP project

The problem is CurrentVersion AbsoluteLayout covered the MainContentView, please go to MainPageView and disable CurrentVersion like the following. the TapGestureRecognizer will work.

<AbsoluteLayout Grid.Row="0" IsEnabled="False">
    <Label
        x:Name="CurrentVersion"
        AbsoluteLayout.LayoutBounds="0,0,75,75"
        AbsoluteLayout.LayoutFlags="PositionProportional" />
    <StackLayout AbsoluteLayout.LayoutBounds="1,0,100,100" AbsoluteLayout.LayoutFlags="PositionProportional">
        <Image x:Name="BrandImage" />
    </StackLayout>
</AbsoluteLayout>
Related