Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FadeAnimation.cs
1using System;
2using System.Runtime.ExceptionServices;
3using System.Threading;
4using System.Threading.Tasks;
5using Microsoft.Maui.ApplicationModel;
6using Microsoft.Maui.Controls;
7
9{
13 public sealed class FadeAnimation : TimedViewAnimation
14 {
15 private readonly double? fromOpacity;
16 private readonly double toOpacity;
17
25 public FadeAnimation(TimeSpan Duration, Easing Easing, double? FromOpacity, double ToOpacity)
26 : base(Duration, Easing)
27 {
28 if (ToOpacity < 0 || ToOpacity > 1)
29 throw new ArgumentOutOfRangeException(nameof(ToOpacity), "Opacity must be between 0 and 1.");
30 if (FromOpacity.HasValue && (FromOpacity.Value < 0 || FromOpacity.Value > 1))
31 throw new ArgumentOutOfRangeException(nameof(FromOpacity), "Opacity must be between 0 and 1.");
32
33 this.fromOpacity = FromOpacity;
34 this.toOpacity = ToOpacity;
35 }
36
38 public override async Task RunAsync(VisualElement Target, IAnimationContext Context, AnimationOptions? Options, CancellationToken Token)
39 {
40 ArgumentNullException.ThrowIfNull(Target);
41 ArgumentNullException.ThrowIfNull(Context);
42
43 if (Context.ReduceMotion && (Options?.ForceMotion != true))
44 {
45 Target.Opacity = this.toOpacity;
46 return;
47 }
48
49 if (this.fromOpacity.HasValue)
50 Target.Opacity = this.fromOpacity.Value;
51
52 uint DurationMs = this.ResolveDurationMilliseconds(Context, Options);
53 Func<CancellationToken, Task> Execute = _ => Target.FadeToAsync(this.toOpacity, DurationMs, this.Easing);
54 ExceptionDispatchInfo? DispatchInfo = null;
55 try
56 {
57 await AnimationRunner.RunAsync(Target, Execute, Token);
58 }
59 catch (Exception Ex)
60 {
61 DispatchInfo = ExceptionDispatchInfo.Capture(Ex);
62 }
63 finally
64 {
65 await this.ApplyFinalOpacityAsync(Target);
66 }
67
68 DispatchInfo?.Throw();
69 }
70
71 private Task ApplyFinalOpacityAsync(VisualElement Target)
72 {
73 return MainThread.InvokeOnMainThreadAsync(() =>
74 {
75 Target.Opacity = this.toOpacity;
76 });
77 }
78 }
79}
Options applied for an individual animation execution.
bool ForceMotion
Gets or sets a value indicating whether motion should run even when global settings request reduced m...
Helper class providing consistent animation execution semantics.
static async Task RunAsync(VisualElement Target, Func< CancellationToken, Task > Execute, CancellationToken Token)
Executes an animation while wiring cancellation tokens to MAUI animation APIs.
Represents an opacity animation.
override async Task RunAsync(VisualElement Target, IAnimationContext Context, AnimationOptions? Options, CancellationToken Token)
Executes the animation against the specified target. Task tracking execution.
FadeAnimation(TimeSpan Duration, Easing Easing, double? FromOpacity, double ToOpacity)
Initializes a new instance of the FadeAnimation class.
Base class providing timing helpers for view animations.
uint ResolveDurationMilliseconds(IAnimationContext Context, AnimationOptions? Options)
Resolves the duration expressed in milliseconds.
Represents contextual information required when executing an animation.
bool ReduceMotion
Gets a value indicating whether the caller requests reduced motion.
Definition: ImplTypes.g.cs:58