This repository has been archived by the owner on May 1, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically marshal all AnimationExtensions calls onto UI thread (#48)
- Loading branch information
Showing
4 changed files
with
217 additions
and
63 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39821.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Xamarin.Forms.CustomAttributes; | ||
|
||
#if UITEST | ||
using Xamarin.UITest; | ||
using NUnit.Framework; | ||
#endif | ||
|
||
namespace Xamarin.Forms.Controls.Issues | ||
{ | ||
[Preserve(AllMembers = true)] | ||
[Issue(IssueTracker.Bugzilla, 39821, "ViewExtension.TranslateTo cannot be invoked on Main thread")] | ||
public class Bugzilla39821 : TestContentPage | ||
{ | ||
protected override void Init() | ||
{ | ||
var box = new BoxView { BackgroundColor = Color.Blue, WidthRequest = 50, HeightRequest = 50, HorizontalOptions = LayoutOptions.Center }; | ||
|
||
var instructions = new Label { Text = "Click the 'Animate' button to run animation on the box. If the animations complete without crashing, this test has passed." }; | ||
|
||
var success = new Label { Text = "Success", IsVisible = false }; | ||
|
||
var button = new Button() { Text = "Animate" }; | ||
|
||
Content = new StackLayout | ||
{ | ||
VerticalOptions = LayoutOptions.Fill, | ||
HorizontalOptions = LayoutOptions.Fill, | ||
Children = | ||
{ | ||
instructions, | ||
success, | ||
button, | ||
new AbsoluteLayout | ||
{ | ||
Children = { box }, | ||
HorizontalOptions = LayoutOptions.Fill, | ||
VerticalOptions = LayoutOptions.Fill | ||
} | ||
} | ||
}; | ||
|
||
button.Clicked += async (sender, args) => { | ||
// Run a bunch of animations from the thread pool | ||
await Task.WhenAll( | ||
Task.Run(async () => await Translate(box)), | ||
Task.Run(async () => await CheckTranslateRunning(box)), | ||
Task.Run(async () => await AnimateScale(box)), | ||
Task.Run(async () => await Rotate(box)), | ||
Task.Run(async () => await Animate(box)), | ||
Task.Run(async () => await Kinetic(box)), | ||
Task.Run(async () => await Cancel(box)) | ||
); | ||
success.IsVisible = true; | ||
}; | ||
} | ||
|
||
async Task CheckTranslateRunning(BoxView box) | ||
{ | ||
Debug.WriteLine(box.AnimationIsRunning("TranslateTo") ? "Translate is running" : "Translate is not running"); | ||
} | ||
|
||
static async Task Translate(BoxView box) | ||
{ | ||
var currentX = box.X; | ||
var currentY = box.Y; | ||
|
||
await box.TranslateTo(currentX, currentY + 100); | ||
await box.TranslateTo(currentX, currentY); | ||
} | ||
|
||
static async Task AnimateScale(BoxView box) | ||
{ | ||
await box.ScaleTo(2); | ||
await box.ScaleTo(0.5); | ||
} | ||
|
||
static async Task Rotate(BoxView box) | ||
{ | ||
await box.RelRotateTo(360); | ||
} | ||
|
||
async Task Cancel(BoxView box) | ||
{ | ||
box.AbortAnimation("animate"); | ||
box.AbortAnimation("kinetic"); | ||
} | ||
|
||
async Task Animate(BoxView box) | ||
{ | ||
box.Animate("animate", d => d, d => { }, 100, 1); | ||
} | ||
|
||
async Task Kinetic(BoxView box) | ||
{ | ||
var resultList = new List<Tuple<double, double>>(); | ||
|
||
box.AnimateKinetic("kinetic", (distance, velocity) => | ||
{ | ||
resultList.Add(new Tuple<double, double>(distance, velocity)); | ||
return true; | ||
}, 100, 1); | ||
} | ||
|
||
#if UITEST | ||
[Test] | ||
public void DoesNotCrash() | ||
{ | ||
RunningApp.Tap(q => q.Marked("Animate")); | ||
RunningApp.WaitForElement(q => q.Marked("Success")); | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters