/// <summary> /// Checks if the manipulation should cause a tilt, and if so starts the /// tilt effect. /// </summary> /// <param name="source">The source of the manipulation (the tilt /// container, eg entire page).</param> /// <param name="e">The args from the ManipulationStarted event.</param> static void TryStartTiltEffect(FrameworkElement source, ManipulationStartedEventArgs e) { #if WP7 ContentPresenter lastContentPresenter = null; #endif foreach (FrameworkElement ancestor in (e.OriginalSource as FrameworkElement).GetVisualAncestors()) { #if WP7 var contentPresenter = ancestor as ContentPresenter; if (contentPresenter != null) { lastContentPresenter = contentPresenter; } #endif foreach (Type t in TiltableItems) { if (t.IsAssignableFrom(ancestor.GetType())) { FrameworkElement elementSuppressingTilt = null; // Look up the tree to find either an explicit DO or DO NOT suppress. if (ancestor.ReadLocalValue(SuppressTiltProperty) is bool) { elementSuppressingTilt = ancestor; } else { elementSuppressingTilt = ancestor.GetVisualAncestors().FirstOrDefault(x => x.ReadLocalValue(SuppressTiltProperty) is bool); } if (elementSuppressingTilt != null && (bool)elementSuppressingTilt.ReadLocalValue(SuppressTiltProperty) == true) { continue; } else { #if WP7 // Check whether the first child of a ListBoxItem's ContentPresenter has SuppressTilt set to true if (t == typeof(ListBoxItem) && lastContentPresenter != null) { var childElement = lastContentPresenter.GetVisualChildren().FirstOrDefault() as FrameworkElement; if (childElement != null) { object suppressTilt = childElement.ReadLocalValue(SuppressTiltProperty); if (suppressTilt is bool && (bool)suppressTilt == true) { return; } } } #endif #if WP8 if (t == typeof(LongListSelector)) { StartTiltEffectOnLLS((LongListSelector)ancestor, e); } else { #endif // Use first child of the control, so that we can add transforms and not // impact any transforms on the control itself. FrameworkElement element = VisualTreeHelper.GetChild(ancestor, 0) as FrameworkElement; FrameworkElement container = e.ManipulationContainer as FrameworkElement; if (element == null || container == null) { return; } // Touch point relative to the element being tilted. Point tiltTouchPoint = container.TransformToVisual(element).Transform(e.ManipulationOrigin); // Center of the element being tilted. Point elementCenter = new Point(element.ActualWidth / 2, element.ActualHeight / 2); // Camera adjustment. Point centerToCenterDelta = GetCenterToCenterDelta(element, source); BeginTiltEffect(element, tiltTouchPoint, elementCenter, centerToCenterDelta); #if WP8 } #endif return; } } } } }