protected override void OnDisconnected(UIElement oldElement) { // Dispose Light and Composition resources when it is removed from the tree HoverLight.RemoveTargetElement(GetId(), oldElement); CompositionLight.Dispose(); _lightPositionExpression.Dispose(); _offsetAnimation.Dispose(); }
protected override void OnConnected(UIElement targetElement) { Compositor compositor = Window.Current.Compositor; // Create SpotLight and set its properties SpotLight spotLight = compositor.CreateSpotLight(); spotLight.InnerConeAngleInDegrees = 50f; spotLight.InnerConeColor = Colors.FloralWhite; spotLight.OuterConeAngleInDegrees = 0f; spotLight.ConstantAttenuation = 1f; spotLight.LinearAttenuation = 0.253f; spotLight.QuadraticAttenuation = 0.58f; // Associate CompositionLight with XamlLight CompositionLight = spotLight; // Define resting position Animation Vector3 restingPosition = new Vector3(200, 200, 400); CubicBezierEasingFunction cbEasing = compositor.CreateCubicBezierEasingFunction(new Vector2(0.3f, 0.7f), new Vector2(0.9f, 0.5f)); _offsetAnimation = compositor.CreateVector3KeyFrameAnimation(); _offsetAnimation.InsertKeyFrame(1, restingPosition, cbEasing); _offsetAnimation.Duration = TimeSpan.FromSeconds(0.5f); spotLight.Offset = restingPosition; // Define expression animation that relates light's offset to pointer position CompositionPropertySet hoverPosition = ElementCompositionPreview.GetPointerPositionPropertySet(targetElement); _lightPositionExpression = compositor.CreateExpressionAnimation("Vector3(hover.Position.X, hover.Position.Y, height)"); _lightPositionExpression.SetReferenceParameter("hover", hoverPosition); _lightPositionExpression.SetScalarParameter("height", 15.0f); // Configure pointer entered/ exited events targetElement.PointerMoved += TargetElement_PointerMoved; targetElement.PointerExited += TargetElement_PointerExited; // Add UIElement to the Light's Targets HoverLight.AddTargetElement(GetId(), targetElement); }