More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, 20 October 2019 6:31:03 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 519

Xamarin Forms - How to spin a visual element

       

Code:
public async Task Spin(VisualElement element)
        {
            if (element != null)
            {
                await StartSpinTimer(new CancellationTokenSource(), element);

                if (animateTimerCancellationTokenSource != null)                 {                     animateTimerCancellationTokenSource.Cancel();                     animateTimerCancellationTokenSource.Dispose();                     animateTimerCancellationTokenSource = null;                 }                 await element.RotateTo(0, 0); // reset to initial position             }         }

        private async Task StartSpinTimer(CancellationTokenSource tokenSource, VisualElement element)         {             try             {                 //maintain a reference to the token so we can cancel when needed                 animateTimerCancellationTokenSource = tokenSource;

                int idleTime = 10; //ms                 await Task.Delay(TimeSpan.FromMilliseconds(idleTime), tokenSource.Token);

                //Do something here                 Device.BeginInvokeOnMainThread(async () =>                 {                     await element.RelRotateTo(360, 800, Easing.Linear);                     if (IsSpinning)                     {                         //Do this if you want to have it possibly happen again                         await StartSpinTimer(new CancellationTokenSource(), element);                     }                 });             }             catch (TaskCanceledException ex)             {                 //if we cancel/reset, this catch block gets called                 Debug.WriteLine(ex);             }             // if we reach here, this timer has stopped         }

Here we use a button for example. The button can be referenced via Command Interface in one of the following two ways.

1.

Code:
<views:CustomImageButton x:Name="buttonToSpin" Source="{Binding TheIcon}" 
    Command="{Binding TheCommand}"
    CommandParameter="{x:Reference buttonToSpin}"/>

2.

Code:
<StackLayout.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TheCommand}" CommandParameter="{x:Reference buttonToSpin}" />
</StackLayout.GestureRecognizers>

CommandParameter has a reference to buttonToSpin. It is the visual element in the example code above which will be rotated through RelRotateTo.

Edited by user Monday, 21 October 2019 5:42:39 PM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.