在xamarin.forms中使用Detect Shake功能时,UI会调用多次



我正在使用xamarin.forms shake delect功能,在摇晃手机时,我会调用弹出屏幕,但该屏幕会调用多次,直到我无法停止摇晃手机

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调用了很多次请帮助

提前感谢

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

最新更新