【问题标题】:On using Detect Shake functionality in xamarin.forms UI is calling multiple times在 xamarin.forms UI 中使用 Detect Shake 功能多次调用
【发布时间】:2020-11-25 11:47:18
【问题描述】:

我正在使用 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 会调用多次 请帮忙

提前致谢

【问题讨论】:

    标签: xamarin.forms popup xamarin.essentials


    【解决方案1】:

    删除您的 trycatch 块并实现 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.
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多