0

我正在使用 xamarin.forms 摇动选择功能,在摇动手机时我正在调用弹出屏幕,但该屏幕会多次调用,直到我不会停止摇动手机

RateUsPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<pages:PopupPage  Title="Rate Us"
                  BackgroundColor="{DynamicResource TransparentPurple}"
                  Padding="0">
    <pages:PopupPage.Animation>
        <animations:ScaleAnimation PositionIn="Center" PositionOut="Center" ScaleIn="1.2" ScaleOut="0.8" DurationIn="400" DurationOut="300" EasingIn="SinOut" EasingOut="SinIn" HasBackgroundAnimation="True" />
    </pages:PopupPage.Animation>
    <StackLayout VerticalOptions="Center" HorizontalOptions="Center" Margin="20">
        <StackLayout>
            <Label Text="Rate Your Experience !!!" FontSize="{DynamicResource FontSize14}"/>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Source="{Binding Star1}" Grid.Row="0" Grid.Column="0">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding StarTappedCommand}" CommandParameter="1"/>
                    </Image.GestureRecognizers>
                </Image>
                <Image Source="{Binding Star2}" Grid.Row="0" Grid.Column="1">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding StarTappedCommand}" CommandParameter="2"/>
                    </Image.GestureRecognizers>
                </Image>
                <Image Source="{Binding Star3}" Grid.Row="0" Grid.Column="2">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding StarTappedCommand}" CommandParameter="3"/>
                    </Image.GestureRecognizers>
                </Image>
                <Image Source="{Binding Star4}" Grid.Row="0" Grid.Column="3">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding StarTappedCommand}" CommandParameter="4"/>
                    </Image.GestureRecognizers>
                </Image>
                <Image Source="{Binding Star5}" Grid.Row="0" Grid.Column="4">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding StarTappedCommand}" CommandParameter="5"/>
                    </Image.GestureRecognizers>
                </Image>
            </Grid>
        </StackLayout>
        <StackLayout>
            <Grid Padding="10" RowSpacing="0" ColumnSpacing="15" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                <Button Padding="0" Grid.Row="0" Grid.Column="0" Command="{Binding CloseCommand}" FontSize="{DynamicResource FontSize14}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Text="{Binding CancelText}"/>
            </Grid>
        </StackLayout>
    </StackLayout>
</pages:PopupPage>

RateUsPageViewModel.cs

public class RateUsPageViewModel{
     public RateUsPageViewModel(){
          try
               {
                    if (Accelerometer.IsMonitoring)
                         Accelerometer.Stop();
                    else
                         Accelerometer.Start(SensorSpeed.Game);
               }
          catch (FeatureNotSupportedException fnsEx)
               {
                    // Feature not supported on device
               }
               catch (Exception ex)
               {
                    // Other error has occurred.
               }
               Accelerometer.ShakeDetected += Accelerometer_ShakeDetected;
        }
        private void Accelerometer_ShakeDetected(object sender, EventArgs e)
        {
             MainThread.BeginInvokeOnMainThread(() =>
             {
                  _navigationService.ShowPopup<RatingUsPageViewModel>();
              });
        }
    }

因此,当我使用此代码时,我的 UI 调用次数请帮忙

提前致谢

4

1 回答 1

0

删除你的tryandcatch块并实现ToggleAccelerometer()方法。在构造函数中调用ToggleAccelerometer()方法,然后再次调用Accelerometer_ShakeDetected(object sender, EventArgs e). 它应该可以解决您的问题。

    public class RateUsPageViewModel
    {
        public RateUsPageViewModel()
        {
            ToggleAccelerometer();
            Accelerometer.ShakeDetected += Accelerometer_ShakeDetected;
        }

        private void Accelerometer_ShakeDetected(object sender, EventArgs e)
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                ToggleAccelerometer();
                _navigationService.ShowPopup<RatingUsPageViewModel>();
            });
        }
        public void ToggleAccelerometer()
        {
            try
            {
                if (Accelerometer.IsMonitoring)
                    Accelerometer.Stop();
                else
                    Accelerometer.Start(speed);
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Feature not supported on device
            }
            catch (Exception ex)
            {
                // Other error has occurred.
            }
        }
    }
于 2020-11-25T16:56:14.857 回答