|
| 1 | +//********************************************************* |
| 2 | +// |
| 3 | +// Copyright (c) Microsoft. All rights reserved. |
| 4 | +// This code is licensed under the MIT License (MIT). |
| 5 | +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 6 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 7 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 8 | +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 9 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 10 | +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
| 11 | +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12 | +// |
| 13 | +//********************************************************* |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Linq; |
| 17 | +using System.Numerics; |
| 18 | +using Windows.UI; |
| 19 | +using Windows.UI.Composition; |
| 20 | +using Windows.UI.Xaml; |
| 21 | +using Windows.UI.Xaml.Hosting; |
| 22 | + |
| 23 | +namespace CompositionSampleGallery |
| 24 | +{ |
| 25 | + public sealed partial class AnimationCompleted : SamplePage |
| 26 | + { |
| 27 | + private SpriteVisual _visual; |
| 28 | + private CompositionColorBrush _brush; |
| 29 | + private Compositor _compositor; |
| 30 | + private ScalarKeyFrameAnimation _showAnimation; |
| 31 | + private ScalarKeyFrameAnimation _hideAnimation; |
| 32 | + private CompositionScopedBatch _batch; |
| 33 | + private bool _isHidden; |
| 34 | + |
| 35 | + public AnimationCompleted() |
| 36 | + { |
| 37 | + this.InitializeComponent(); |
| 38 | + Setup(); |
| 39 | + } |
| 40 | + |
| 41 | + public static string StaticSampleName => "Animation Completed"; |
| 42 | + public override string SampleName => StaticSampleName; |
| 43 | + public static string StaticSampleDescription => "This sample demonstrates how to get a notification and clean up resources when a CompositionAnimation completes"; |
| 44 | + public override string SampleDescription => StaticSampleDescription; |
| 45 | + public override string SampleCodeUri => "http://go.microsoft.com/fwlink/p/?LinkID=761160"; |
| 46 | + |
| 47 | + private void Setup() |
| 48 | + { |
| 49 | + var gridVisual = ElementCompositionPreview.GetElementVisual(MainGrid); |
| 50 | + _compositor = gridVisual.Compositor; |
| 51 | + |
| 52 | + _showAnimation = _compositor.CreateScalarKeyFrameAnimation(); |
| 53 | + _showAnimation.InsertKeyFrame(1.0f, 1.0f); |
| 54 | + _showAnimation.Duration = TimeSpan.FromSeconds(1); |
| 55 | + |
| 56 | + _hideAnimation = _compositor.CreateScalarKeyFrameAnimation(); |
| 57 | + _hideAnimation.InsertKeyFrame(1.0f, 0.0f); |
| 58 | + _hideAnimation.Duration = TimeSpan.FromSeconds(1); |
| 59 | + |
| 60 | + _isHidden = false; |
| 61 | + |
| 62 | + CreateVisualTree(); |
| 63 | + } |
| 64 | + private void CreateVisualTree() |
| 65 | + { |
| 66 | + _visual = _compositor.CreateSpriteVisual(); |
| 67 | + _visual.Size = new Vector2(300f); |
| 68 | + _brush = _compositor.CreateColorBrush(Colors.Blue); |
| 69 | + _visual.Brush = _brush; |
| 70 | + ElementCompositionPreview.SetElementChildVisual(MainGrid, _visual); |
| 71 | + } |
| 72 | + private void ShowButton_Click(object sender, RoutedEventArgs e) |
| 73 | + { |
| 74 | + if (_isHidden) |
| 75 | + { |
| 76 | + CreateVisualTree(); |
| 77 | + _visual.Opacity = 0.0f; |
| 78 | + _visual.StartAnimation("Opacity", _showAnimation); |
| 79 | + _isHidden = false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void HideButton_Click(object sender, RoutedEventArgs e) |
| 84 | + { |
| 85 | + if (!_isHidden) |
| 86 | + { |
| 87 | + _batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); |
| 88 | + _batch.Completed += OnBatchCompleted; |
| 89 | + _visual.StartAnimation("Opacity", _hideAnimation); |
| 90 | + _batch.End(); |
| 91 | + _isHidden = true; |
| 92 | + } |
| 93 | + } |
| 94 | + private void OnBatchCompleted(object sender, CompositionBatchCompletedEventArgs args) |
| 95 | + { |
| 96 | + _visual.Dispose(); |
| 97 | + _brush.Dispose(); |
| 98 | + _visual = null; |
| 99 | + _brush = null; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments