我想在我的Xamarin.Forms應用程序中獲得gps位置(經度和緯度)。我正在使用Geolocator插件,它至少需要5秒才能獲得gps位置。在此期間,我想創建加載動畫。我做到了,但我無法阻止動畫。下面代碼的效果是 - >https://www.youtube.com/watch?v=MBf_VzaqkOQ&feature=youtu.be。Xamarin.forms動畫
async void Button_Clicked(object sender, EventArgs e)
{
Plugin.Geolocator.Abstractions.Position position = new Plugin.Geolocator.Abstractions.Position();
if(!locationProvider.IsGpsAvailable()){
//...
}
else
{
try
{
btnGetLocation.IsEnabled = false;
settingsLayout.IsVisible = true;
labelLatitude.Text = string.Empty;
labelLongitude.Text = string.Empty;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 0.5;
CancellationTokenSource cancellationTokenSoruce = new CancellationTokenSource();
CancellationToken ct = cancellationTokenSoruce.Token;
image1.Scale = 0;
image2.Scale = 0;
image3.Scale = 0;
Task.Run(async() =>
{
for (int i = 0; i < 20; i++)
{
await image1.ScaleTo(1, 400, Easing.Linear);
await image1.ScaleTo(0, 400, Easing.Linear);
await image1.TranslateTo(image1.TranslationX, image1.TranslationY, 600, Easing.Linear);
}
}, ct);
Task.Run(async() =>
{
for (int i = 0; i < 20; i++)
{
await image2.TranslateTo(image2.TranslationX, image2.TranslationY, 200, Easing.Linear);
await image2.ScaleTo(1, 400, Easing.Linear);
await image2.ScaleTo(0, 400, Easing.Linear);
await image2.TranslateTo(image2.TranslationX, image2.TranslationY, 400, Easing.Linear);
}
}, ct);
Task.Run(async() =>
{
for (int i = 0; i < 20; i++)
{
await image3.TranslateTo(image3.TranslationX, image3.TranslationY, 400, Easing.Linear);
await image3.ScaleTo(1, 400, Easing.Linear);
await image3.ScaleTo(0, 400, Easing.Linear);
await image3.TranslateTo(image3.TranslationX, image3.TranslationY, 200, Easing.Linear);
}
}, ct);
position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
cancellationTokenSoruce.Cancel();
ViewExtensions.CancelAnimations(image1);
ViewExtensions.CancelAnimations(image2);
ViewExtensions.CancelAnimations(image3);
}
catch
{
}
image1.Scale = 0;
image2.Scale = 0;
image3.Scale = 0;
labelLatitude.Text = position.Latitude.ToString();
labelLongitude.Text = position.Longitude.ToString();
//settingsLayout.IsVisible = false;
btnGetLocation.IsEnabled = true;
}
}
ViewExtensions.CancelAnimations(image);應該停止動畫,但它不起作用,取消令牌應停止任務中的方法。這段代碼有什麼問題?
編輯:我忘了XAML文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinFormsOne.Pages.GPSOwnTestPage"
BackgroundColor="{StaticResource PageBackgroundColor}">
<ContentPage.Content>
<StackLayout>
<Button x:Name="btnGetLocation"
Text="Get GPS Avability"
Clicked="Button_Clicked"/>
<Label x:Name="labelLatitude"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
TextColor="White"/>
<Label x:Name="labelLongitude"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
TextColor="White"/>
<StackLayout Orientation="Horizontal" x:Name="settingsLayout">
<Grid WidthRequest="120"/>
<Image x:Name="image1"
Source="settingsIcon.png"
HorizontalOptions="EndAndExpand"
VerticalOptions="EndAndExpand"/>
<Image x:Name="image2"
Source="settingsIcon.png"
HorizontalOptions="CenterAndExpand"
VerticalOptions="EndAndExpand"/>
<Image x:Name="image3"
Source="settingsIcon.png"
HorizontalOptions="StartAndExpand"
VerticalOptions="EndAndExpand"/>
<Grid WidthRequest="120"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
是的,你的方式很好。謝謝 – lukhol