本文整理汇总了C#中System.Windows.FrameworkElement.AncestorsAndSelf方法的典型用法代码示例。如果您正苦于以下问题:C# FrameworkElement.AncestorsAndSelf方法的具体用法?C# FrameworkElement.AncestorsAndSelf怎么用?C# FrameworkElement.AncestorsAndSelf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.FrameworkElement
的用法示例。
在下文中一共展示了FrameworkElement.AncestorsAndSelf方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindFrame
/// <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;
}
示例2: FindFrame
/// <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;
}