On using Detect Shake functionality in xamarin.forms UI is calling multiple times

306 views Asked by At

I am working with xamarin.forms shake delect functionality, where on shaking the mobile I am calling a popup screen, but that screen is calling multiple times till I wont stop shaking the phone

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>();
              });
        }
    }

so when I use this code my UI is calling number of times please help

thanks in advance

1

There are 1 answers

0
Vijay On

Remove your try and catch blocks and implement ToggleAccelerometer() method. call ToggleAccelerometer() method in your constructor and then again call in Accelerometer_ShakeDetected(object sender, EventArgs e). It should stop your problem.

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