在 UWP 项目的 contentView 中无法触发 TapGestureRecognizer 的“tapped”方法

Posted

技术标签:

【中文标题】在 UWP 项目的 contentView 中无法触发 TapGestureRecognizer 的“tapped”方法【英文标题】:the "tapped" method of TapGestureRecognizer can not be fired in contentView in UWP project 【发布时间】:2021-10-23 16:48:42 【问题描述】:

我有一个 xamarin.form 的旧项目。在win10 2018版本可以正常工作。但是windows更新到最新版本后,UWP exe文件无法在windows系统中安装。我正在尝试修复这个项目。 现在我使用的是 VS2019,Xamarin.Forms 是最新的(5.0.0.2083),Microsoft.netCore.Platforms 是 5.0.2。该解决方案可以通过一些warings构建,并且可以直接在本地win10上以debug模式运行。

应用由一个Main page frame组成,frame内有多个contentView。现在我遇到了 TapGestureRecognizer 在 contentView 中不起作用的问题。但它可以在MainPage中正常工作。 这是 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>

cs 文件:

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!");
        
    

在 .cs 文件中,方法 ContinueBtn_OnClicked 根本不会被调用。单击 StackLayout 时没有任何反应。

但是我注意到MainPageView.xaml中frame的TapGestureRecognizer可以正常工作:

            <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>

在此页面中,可以调用“Button_OnViewSelected”方法。 MainPage 是 ContentPage 但 FirstStartView 是 ContentView 如果结果不同的话。 我还注意到在 ContentView 中,所有组件都不能触发相关方法。在我的帖子代码中,SwitchCell 无法运行 OnChanged(),其他标签无法运行 OnClicked() 等等。 点击标签或更改switchCell或TapGestureRecognizer时没有错误,只是没有发生任何事情。

我怀疑 xaml 和 cs 之间是否有某些绑定路径被破坏;但我不擅长 Xamarin.Form。所以我在这里请求一些建议。

谢谢大家。

【问题讨论】:

我尝试使用上述代码制作代码示例,StackLayout TapGestureRecognizer 可以按预期工作,我们无法重现您的问题,您能否介意分享minimal reproducible example,我将在此基础上进行测试。 感谢您的回复。我把我的项目放在github上:github.com/airixz/demo-test。该解决方案包括一个可移植项目和一个 uwp 项目。可移植项目/视图文件夹中的问题代码。 MainPageView 是一个框架页面,在屏幕底部包含五个“按钮”。这五个按钮使用 TapGestureRecognizer 来响应“点击”,它起作用了。但在同一文件夹中的其他视图(例如StartView.xaml 或 FirstStartView.xaml) TapGestureRecognizer 根本没有响应。并且“SwitchCell”也不起作用。似乎框架中的所有组件(content.view) 都无法触发。 好的,我会测试,请给我秒 解决方案可以vs2019搭建,win10运行。没有错误报告,我在 OnTapped 函数中添加了一些日志。但它不是由鼠标调用的。 【参考方案1】:

UWP项目的contentView中无法触发TapGestureRecognizer的“tapped”方法

问题是CurrentVersionAbsoluteLayout覆盖了MainContentView,请转到MainPageView并禁用CurrentVersion,如下所示。 TapGestureRecognizer 将起作用。

<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>

【讨论】:

以上是关于在 UWP 项目的 contentView 中无法触发 TapGestureRecognizer 的“tapped”方法的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Xamarin Forms 中启动 UWP 项目

我无法在codedui中选择UWP ListView项目

UWP应用程序无法在VS 2017中运行

无法在发布模式下构建 UWP

Visual Studio 2015 无法识别 OTF 转换字体 (UWP)

UWP锁解屏后无法响应操作