/// <summary> /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">The frame name.</param> /// <param name="context">The framework element providing the context for finding a frame.</param> /// <returns>The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException("context"); } else if (context is ModernWindow) { context = ((ModernWindow)context).ContentFrame; } // collect all ancestor frames ModernFrame[] frames = context.AncestorsAndSelf().OfType <ModernFrame>().ToArray(); if (name == null || name == FrameSelf) { // find first ancestor frame return(frames.FirstOrDefault()); } if (name == FrameParent) { // find parent frame return(frames.Skip(1).FirstOrDefault()); } if (name == FrameTop) { // find top-most frame return(frames.LastOrDefault()); } // find ancestor frame having a name matching the target ModernFrame frame = frames.FirstOrDefault(f => f.Name == name); if (frame == null) { // find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame == null) { // find frame in scope of ancestor frame content ModernFrame parent = frames.FirstOrDefault(); if (parent != null && parent.Content != null) { FrameworkElement content = parent.Content as FrameworkElement; if (content != null) { frame = content.FindName(name) as ModernFrame; } } } } return(frame); }
#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high /// <summary> /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">The frame name.</param> /// <param name="context">The framework element providing the context for finding a frame.</param> /// <returns>The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) #pragma warning restore S3776 // Cognitive Complexity of methods should not be too high { if (context == null) { throw new ArgumentNullException(nameof(context)); } // collect all ancestor frames ModernFrame[] frames = context.AncestorsAndSelf().OfType <ModernFrame>().ToArray(); if (name == null || name == FrameSelf) { // find first ancestor frame return(frames.FirstOrDefault()); } if (name == FrameParent) { // find parent frame return(frames.Skip(1).FirstOrDefault()); } if (name == FrameTop) { // find top-most frame return(frames.LastOrDefault()); } // find ancestor frame having a name matching the target ModernFrame frame = frames.FirstOrDefault(f => f.Name == name); if (frame == null) { // find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame == null) { // find frame in scope of ancestor frame content ModernFrame parent = frames.FirstOrDefault(); if (parent != null && parent.Content != null) { FrameworkElement content = parent.Content as FrameworkElement; if (content != null) { frame = content.FindName(name) as ModernFrame; } } } } return(frame); }
/// <summary> /// 查找在指定上下文中使用给定名称标识的框架 /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">框架名字 The frame name.</param> /// <param name="context">提供查找框架上下文的框架元素 The framework element providing the context for finding a frame.</param> /// <returns>如果找不到该框架,则为空 The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException("context"); } // 收集所有祖先框架 collect all ancestor frames var frames = context.AncestorsAndSelf().OfType <ModernFrame>().ToArray(); if (name == null || name == FrameSelf) { // 查找第一个祖先框架 find first ancestor frame return(frames.FirstOrDefault()); } if (name == FrameParent) { //查找父框架 find parent frame return(frames.Skip(1).FirstOrDefault()); } if (name == FrameTop) { // 查找最上面的框架 find top-most frame return(frames.LastOrDefault()); } // 查找名称与目标匹配的祖先框架 find ancestor frame having a name matching the target var frame = frames.FirstOrDefault(f => f.Name == name); if (frame == null) { // 在上下文范围内查找框架 find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame == null) { // 在祖先框架内容范围内查找框架 find frame in scope of ancestor frame content var parent = frames.FirstOrDefault(); if (parent != null && parent.Content != null) { var content = parent.Content as FrameworkElement; if (content != null) { frame = content.FindName(name) as ModernFrame; } } } } return(frame); }
/// <summary> /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">The frame name.</param> /// <param name="context">The framework element providing the context for finding a frame.</param> /// <returns>The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // collect all ancestor frames var frames = context.AncestorsAndSelf().OfType <ModernFrame>().ToArray(); switch (name) { case null: case FrameSelf: // find first ancestor frame return(frames.FirstOrDefault()); case FrameParent: // find parent frame return(frames.Skip(1).FirstOrDefault()); case FrameTop: // find top-most frame return(frames.LastOrDefault()); } // find ancestor frame having a name matching the target var frame = frames.FirstOrDefault(f => f.Name == name); if (frame != null) { return(frame); } // find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame != null) { return(frame); } // find frame in scope of ancestor frame content var parent = frames.FirstOrDefault(); var content = parent?.Content as FrameworkElement; return(content?.FindName(name) as ModernFrame); }