4

我想创建一个可以找到嵌套树结构深度的属性。下面的静态通过递归找出深度/级别。但是是否可以将此函数作为同一类中的属性而不是静态方法?

public static int GetDepth(MenuGroup contextMenuItems)
{
    if (contextMenuItems == null || contextMenuItems.Items.Count == 0)
        return 0;

    var subMenu = contextMenuItems.Items.Select(b => b as MenuGroup);
    if (!subMenu.Any())
        return 1;

    var subLevel = subMenu.Cast<MenuGroup>().Select(GetDepth);
    return !subLevel.Any() ? 1 : subLevel.Max() + 1;
}

有关代码的更多信息: MenuGroupMenuItem来自MenuBase

MenuGroup具有ObservableCollection<MenuBase> Items作为子元素的子节点

MenuItem是一个没有任何子节点的离开节点。

4

2 回答 2

4

好吧,您可以轻松地将其转换为实例属性,是的:

public int Depth
{
    get
    {
        if (Items.Count == 0)
            return 0;
        var subMenu = Items.Select(b => b as MenuGroup);
        if (!subMenu.Any())
            return 1;
        var subLevel = subMenu.Cast<MenuGroup>().Select(x = > x.Depth);
        return !subLevel.Any() ? 1 : subLevel.Max() + 1;
    }
}

由于非项目的处理,这还不能很好地MenuGroup工作,但它可以很容易地修复,使用OfType而不是Selectthen Cast

public int Depth
{
    get
    {
        // Completely empty menu (not even any straight items). 0 depth.
        if (Items.Count == 0)
        {
            return 0;
        }
        // We've either got items (which would give us a depth of 1) or
        // items and groups, so find the maximum depth of any subgroups,
        // and add 1.
        return Items.OfType<MenuGroup>()
                    .Select(x => x.Depth)
                    .DefaultIfEmpty() // 0 if we have no subgroups
                    .Max() + 1;
    }
}
于 2013-08-20T05:50:14.647 回答
0
public string GenerateMenu()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<nav id=\"nvMenu\" class=\"main-nav\"><ul>");
    sb.Append(PrepareMenuUL(AppConfig._AppConfigInstance.Navigation.FirstOrDefault().NavigationClass));
    sb.Append("</ul></nav>");
    return sb.ToString();
}

private string PrepareMenuUL(List<Navigation> navigation)
{
    StringBuilder sb = new StringBuilder();

    if (Liflag == 1)
    {
        sb.Append("</li>");
        Liflag = 0;
    }

    foreach (var item in navigation)
    {
        var subMenu = item.NavigationClass.Select(b => b as Navigation);

        if (subMenu.Any())
        {
            sb.Append("<li class=\"dropdown\">");
            if (subMenu.Any() && item.Url == "#")
                sb.Append(string.Format("<a href=\"{0}\">{1}<i class=\"icon-arrow\"></i></a>", BaseUrl + item.Url, item.Name));
            else if (subMenu.Any() && item.Url != "#" && item.Url != null)
                sb.Append(string.Format("<a href=\"{0}\">{1}<i class=\"icon-rightarrow\"></i></a>", BaseUrl + item.Url, item.Name));
        }
        else
        {
            sb.Append("<li>");
            sb.Append(string.Format("<a href=\"{0}\">{1}</a>", BaseUrl + item.Url, item.Name));
        }

        if (subMenu.Any())
            sb.Append("<ul class=\"wd190\">");

        if (item.NavigationClass.Count > 0)
        {
            Liflag = 1;
            sb.Append(PrepareMenuUL(item.NavigationClass));
        }

        sb.Append("</li>");

        if (subMenu.Any())
            sb.Append("</ul>");
    }

    return sb.ToString();
}
于 2014-03-14T12:23:21.913 回答