2017-07-01 47 views
0

我想在我的Xamarin.Forms應用程序中獲得gps位置(經度和緯度)。我正在使用Geolocator插件,它至少需要5秒才能獲得gps位置。在此期間,我想創建加載動畫。我做到了,但我無法阻止動畫。下面代碼的效果是 - >https://www.youtube.com/watch?v=MBf_VzaqkOQ&feature=youtu.beXamarin.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> 

回答

0

這裏是你可以做什麼

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[] tasks = new Task[3]; 
      tasks[0] = Task.Run(async() => 
      { 
       while (!ct.IsCancellationRequested) 
       { 
        await image1.ScaleTo(1, 400, Easing.Linear); 
        await image1.ScaleTo(0, 400, Easing.Linear); 
        await image1.TranslateTo(image1.TranslationX, image1.TranslationY, 600, Easing.Linear); 
       } 
      }); 

      tasks[1] = Task.Run(async() => 
      { 
       while (!ct.IsCancellationRequested) 
       { 
        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); 
       } 
      }); 

      tasks[2] = Task.Run(async() => 
      { 
       while (!ct.IsCancellationRequested) 
       { 
        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); 
       } 
      }); 

      //position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 
      await Task.Delay(10000); //simulated GetPositionAsync 
      cancellationTokenSoruce.Cancel(); 
      await Task.WhenAll(tasks); 

      //no need in CancelAnimations, it is done when you exited the Task 
      //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; 
    } 
    //} 
0

我解決我的問題是這樣,也許這將是爲別人有用:

async void Button_Clicked(object sender, EventArgs e) 
     { 
      Plugin.Geolocator.Abstractions.Position position = new Plugin.Geolocator.Abstractions.Position(); 

      if(!locationProvider.IsGpsAvailable()){ 
       // 
      } 
      else 
      { 
       CancellationTokenSource cancellationTokenSoruce = new CancellationTokenSource(); 
       CancellationToken ct = cancellationTokenSoruce.Token; 

       image1.Scale = 0; 
       image2.Scale = 0; 
       image3.Scale = 0; 

       labelLatitude.Text = string.Empty; 
       labelLongitude.Text = string.Empty; 

       Content = acquiringLocationLayoutHandler; 

       try 
       { 
        var locator = CrossGeolocator.Current; 
        locator.DesiredAccuracy = 0.5; 

        var t1 = Task.Run(async() => 
        { 
         while(!ct.IsCancellationRequested) 
         { 
          await image1.ScaleTo(1, 400, Easing.Linear); 
          await image1.ScaleTo(0, 400, Easing.Linear); 
          await image1.TranslateTo(image1.TranslationX, image1.TranslationY, 600, Easing.Linear); 

          if (ct.IsCancellationRequested) 
          { 
           ViewExtensions.CancelAnimations(image1); 
          } 
         } 
        }, ct); 

        var t2 = Task.Run(async() => 
        { 
         while (!ct.IsCancellationRequested) 
         { 
          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); 

          if (ct.IsCancellationRequested) 
          { 
           ViewExtensions.CancelAnimations(image2); 
          } 
         } 
        }, ct); 

        var t3 = Task.Run(async() => 
        { 
         while (!ct.IsCancellationRequested) 
         { 
          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); 

          if (ct.IsCancellationRequested) 
          { 
           ViewExtensions.CancelAnimations(image3); 
          } 
         } 
        }, ct); 

        position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 
       } 
       catch(Exception ee) 
       { 
        Debug.WriteLine(ee.Message); 
       } 
       finally 
       { 
        cancellationTokenSoruce.Cancel(); 
       } 

       labelLatitude.Text = position.Latitude.ToString(); 
       labelLongitude.Text = position.Longitude.ToString(); 

       Content = mainLayoutHandler; 
      } 
+0

是的,你的方式很好。謝謝 – lukhol