예제 #1
0
        // This can be called from the three main entry-points (Primary, Secondary, and StartWith)
        private static void InitializeElement(FrameworkElement element)
        {
            if (GetIsInitialized(element))
            {
                return;
            }

            // Set IsInitialized to true to only run this code once per element
            SetIsInitialized(element, true);

#if __UWP__
            // The new way of handling translate animations (see Translation property section):
            // https://blogs.windows.com/buildingapps/2017/06/22/sweet-ui-made-possible-easy-windows-ui-windows-10-creators-update/
            ElementCompositionPreview.SetIsTranslationEnabled(element, true);
#endif

            element
            .Events()
            .LoadedUntilUnloaded
            .Take(1)
            .Subscribe(
                args =>
            {
                // Perform validations on element's attached properties
                Validate(element);

                var startSettings = element.GetSettings(SettingsTarget.StartWith, getStartWithFunc: GetStartWith);

                // If any StartWith settings were specified, apply them
                if (startSettings != null)
                {
                    element.ApplyInitialSettings((AnimationSettings)startSettings);
                }
            },
                ex => Logger.ErrorException($"Error on subscription to the {nameof(FrameworkElement.Loaded)} event of {nameof(FrameworkElement)}", ex)
                );

            element
            .Observe(UIElement.VisibilityProperty)
            .TakeUntil(element.Events().UnloadedMaterialized)
            .Subscribe(
                _ =>
            {
                var isVisible   = element.Visibility == Visibility.Visible;
                var elementGuid = GetElementGuid(element);

                if (isVisible && _actives.GetNextIdleActiveTimeline(elementGuid)?.Timeline is Timeline idle)
                {
                    RunNextAnimation(idle, element);
                }
            },
                ex => Logger.ErrorException($"Error on subscription to the {nameof(FrameworkElement.Visibility)} changes of {nameof(FrameworkElement)}", ex)
                );

#if __UWP__
            element
            .Events()
            .SizeChanged
            .TakeUntil(element.Events().UnloadedMaterialized)
            .Subscribe(
                args =>
            {
                // If the element child is a SpriteVisual, maintain its size so update any effects applied
                if (args.Sender is FrameworkElement elem &&
                    ElementCompositionPreview.GetElementChildVisual(elem) is SpriteVisual sprite)
                {
                    sprite.Size = new Vector2((float)elem.ActualWidth, (float)elem.ActualHeight);
                }
            },
                ex => Logger.ErrorException($"Error on subscription to the {nameof(FrameworkElement.SizeChanged)} event of {nameof(FrameworkElement)}", ex)
                );
#endif
        }