繁体   English   中英

为什么在 UWP 的情况下,网格的 TapGesture 识别器响应缓慢,但在 Android 和 iOS 中工作正常?

[英]Why TapGesture recognizer for Grid is slow to respond in case of UWP but works fine in Android and iOS?

我有一个按钮和一个网格。 Button 绑定到 ExecuteButtonCommand,TapGestureRecognizer for Grid 绑定到 ExecuteGridCommand。 现在,如果我快速快速地按下按钮,对应的 label 会显示所有平台的正确点击计数,即命令代码正在执行点击发生的次数。

但是在网格的情况下,虽然对于 android 和 iOS 这工作得很好。 但是对于 UWP 来说,并不是所有的点击都在执行命令。 示例:如果我快速点击网格,比如说 6 次,则相应的 label 仅显示 3 或 4 个计数,这意味着用于 tapgesture 的命令执行的次数比实际执行的次数少。

这就是我的 ViewModel 中的内容

<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
        <!-- Place new controls here -->
        <Label Text="{Binding ButtonExecutionCount}" HorizontalOptions="Center"/>
        <Button x:Name="ClickButton" Text="ExecuteClick"  HorizontalOptions="Center"
                    Command="{Binding ExecuteButtonCommand}"
                />
        <Label Text="{Binding GridExecutionCount}" HorizontalOptions="Center"/>
        <Grid BackgroundColor="Aquamarine" VerticalOptions="Center" HorizontalOptions="Center">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Label Text="Click this grid" Grid.Column="0" Grid.Row="0" HorizontalOptions="Center"/>
            <Grid.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding ExecuteGridCommand}"></TapGestureRecognizer>
            </Grid.GestureRecognizers>
        </Grid>
    </StackLayout>

这是记录和显示点击次数的绑定视图模型的代码:

public class MainPageViewModel : INotifyPropertyChanged
    {
        public MainPageViewModel()
        {
            ExecuteGridCommand = new Command(ExecuteGridMethod);
            ExecuteButtonCommand = new Command(ExecuteButtonMethod);
        }

        private int _gridExecutionCount;
        public int GridExecutionCount
        {
            get => _gridExecutionCount;
            set
            {
                _gridExecutionCount = value;
                OnPropertyChanged();
            }
        }

        private int _buttonExecutionCount;
        public int ButtonExecutionCount
        {
            get => _buttonExecutionCount;
            set
            {
                _buttonExecutionCount = value;
                OnPropertyChanged();
            }
        }

        public Command ExecuteGridCommand { get; set; }

        public Command ExecuteButtonCommand { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public void ExecuteGridMethod()
        {
            GridExecutionCount++;
        }

        public void ExecuteButtonMethod()
        {
            ButtonExecutionCount++;
        }
    }

在这里,我已经点击了 5 次,按钮计数很好,但对于 UWP 中的网格,实际点击次数要少。

在此处输入图像描述

原因:您的项目中的奇怪行为是由Xamarin.Forms的版本引起的。 这是4.5.x版本中的一个现有问题。

解决方案:该问题已在最新版本( 4.7.x )中修复。 我在上面测试了你的样品并且工作正常。 因此,您只需要将共享项目和平台项目中的 XF 版本更新到最新版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM