This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from microsoft/animationcompleted
Adding AnimationCompleted API Reference sample
- Loading branch information
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
15 changes: 15 additions & 0 deletions
15
SampleGallery/Samples/SDK 10586/AnimationCompleted/AnimationCompleted.xaml
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,15 @@ | ||
<local:SamplePage | ||
x:Class="CompositionSampleGallery.AnimationCompleted" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="using:CompositionSampleGallery" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d"> | ||
<Grid x:Name="MainGrid"> | ||
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center"> | ||
<Button x:Name="ShowButton" Content="Show Content" Click="ShowButton_Click"/> | ||
<Button x:Name="HideButton" Content="Hide Content" Click="HideButton_Click"/> | ||
</StackPanel> | ||
</Grid> | ||
</local:SamplePage> |
102 changes: 102 additions & 0 deletions
102
SampleGallery/Samples/SDK 10586/AnimationCompleted/AnimationCompleted.xaml.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,102 @@ | ||
//********************************************************* | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// This code is licensed under the MIT License (MIT). | ||
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH | ||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
//********************************************************* | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Numerics; | ||
using Windows.UI; | ||
using Windows.UI.Composition; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Hosting; | ||
|
||
namespace CompositionSampleGallery | ||
{ | ||
public sealed partial class AnimationCompleted : SamplePage | ||
{ | ||
private SpriteVisual _visual; | ||
private CompositionColorBrush _brush; | ||
private Compositor _compositor; | ||
private ScalarKeyFrameAnimation _showAnimation; | ||
private ScalarKeyFrameAnimation _hideAnimation; | ||
private CompositionScopedBatch _batch; | ||
private bool _isHidden; | ||
|
||
public AnimationCompleted() | ||
{ | ||
this.InitializeComponent(); | ||
Setup(); | ||
} | ||
|
||
public static string StaticSampleName => "Animation Completed"; | ||
public override string SampleName => StaticSampleName; | ||
public static string StaticSampleDescription => "This sample demonstrates how to get a notification and clean up resources when a CompositionAnimation completes"; | ||
public override string SampleDescription => StaticSampleDescription; | ||
public override string SampleCodeUri => "http://go.microsoft.com/fwlink/p/?LinkID=761160"; | ||
|
||
private void Setup() | ||
{ | ||
var gridVisual = ElementCompositionPreview.GetElementVisual(MainGrid); | ||
_compositor = gridVisual.Compositor; | ||
|
||
_showAnimation = _compositor.CreateScalarKeyFrameAnimation(); | ||
_showAnimation.InsertKeyFrame(1.0f, 1.0f); | ||
_showAnimation.Duration = TimeSpan.FromSeconds(1); | ||
|
||
_hideAnimation = _compositor.CreateScalarKeyFrameAnimation(); | ||
_hideAnimation.InsertKeyFrame(1.0f, 0.0f); | ||
_hideAnimation.Duration = TimeSpan.FromSeconds(1); | ||
|
||
_isHidden = false; | ||
|
||
CreateVisualTree(); | ||
} | ||
private void CreateVisualTree() | ||
{ | ||
_visual = _compositor.CreateSpriteVisual(); | ||
_visual.Size = new Vector2(300f); | ||
_brush = _compositor.CreateColorBrush(Colors.Blue); | ||
_visual.Brush = _brush; | ||
ElementCompositionPreview.SetElementChildVisual(MainGrid, _visual); | ||
} | ||
private void ShowButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
if (_isHidden) | ||
{ | ||
CreateVisualTree(); | ||
_visual.Opacity = 0.0f; | ||
_visual.StartAnimation("Opacity", _showAnimation); | ||
_isHidden = false; | ||
} | ||
} | ||
|
||
private void HideButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
if (!_isHidden) | ||
{ | ||
_batch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation); | ||
_batch.Completed += OnBatchCompleted; | ||
_visual.StartAnimation("Opacity", _hideAnimation); | ||
_batch.End(); | ||
_isHidden = true; | ||
} | ||
} | ||
private void OnBatchCompleted(object sender, CompositionBatchCompletedEventArgs args) | ||
{ | ||
_visual.Dispose(); | ||
_brush.Dispose(); | ||
_visual = null; | ||
_brush = null; | ||
} | ||
} | ||
} |
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