public static string GeneratePathString(DependencyObject element, string keyPrefix, string keySuffix) { // get a listing of the types from SketchFlow Player's model List <string> screenTypeNames = ( from screen in PlayerContext.Instance.RuntimeData.Screens select screen.ClassName) .ToList(); // find the topmost SketchFlow screen DependencyObject topmostScreen = ( from ancestor in DependencyObjectHelper.GetSelfAndVisualAncestors(element) let typeName = ancestor.GetType().FullName where screenTypeNames.Contains(typeName) select ancestor) .LastOrDefault(); if (topmostScreen == null) { return(null); } // get the visual ancestors up to but not including the topmost screen List <DependencyObject> visualHierarchy = DependencyObjectHelper.GetSelfAndVisualAncestors(element) .TakeWhile(visual => visual != topmostScreen) .ToList(); // walk up to it, recording child indices at each level in the visual tree IEnumerable <int> indices = ( from child in visualHierarchy let parent = VisualTreeHelper.GetParent(child) let siblings = DependencyObjectHelper.GetVisualChildren(parent).ToList() let index = siblings.IndexOf(child) select index) .Reverse(); // create the path to the screen string key = string.Format(PathFormatString, keyPrefix, topmostScreen.GetType().FullName); foreach (int index in indices) { key = string.Format(PathFormatString, key, index); } // add the group name key = string.Format(PathFormatString, key, keySuffix); return(key); }
private void SaveState(FrameworkElement element, DependencyProperty property, string propertyName) { if (HasBindingExpression(element, property)) { return; } string key = DependencyObjectHelper.GeneratePathString(element, this.GetType().FullName, element.GetType().FullName + "/" + propertyName); if (string.IsNullOrEmpty(key)) { return; } dataStore[key] = element.GetValue(property); }
private void PreserveState(Action <UserControl, VisualStateGroup, string> preserveStateAction) { UserControl screen = this.AssociatedObject; FrameworkElement content = screen.Content as FrameworkElement; IEnumerable <VisualStateGroup> groups = VisualStateManager.GetVisualStateGroups(content).OfType <VisualStateGroup>(); foreach (VisualStateGroup group in groups) { string key = DependencyObjectHelper.GeneratePathString(screen, this.GetType().FullName, group.Name); if (string.IsNullOrEmpty(key)) { continue; } preserveStateAction(screen, group, key); } }
private void PreserveStateVisitor(Action <FrameworkElement, DependencyProperty, string> preserveStateAction, DependencyObject parent) { FrameworkElement parentElement = parent as FrameworkElement; if (parentElement != null) { foreach (PreservePropertyInfo target in propertiesToPreserve .Where(target => target.Type.IsAssignableFrom(parent.GetType()))) { preserveStateAction(parentElement, target.Property, target.PropertyName); } } foreach (DependencyObject child in DependencyObjectHelper.GetVisualChildren(parent)) { PreserveStateVisitor(preserveStateAction, child); } }