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