如何在Kendo UI MVC菜单中添加Font Awesome图标?

7

我想在kendo UI ASP.NET菜单中添加一个font awesome图标,但是我无法在Kendo的示例中找到如何实现的例子。以下是代码:

           @(Html.Kendo().Menu()
          .Name("PreferencesMenu")
          .HtmlAttributes(new { style = "width: 125px; height:900px; border:0px;" })
          .Direction("down")
          .Orientation(MenuOrientation.Vertical)
          .Items(items =>
          {
              items.Add()
                  .Text("Account");

              items.Add()
                  .Text("Notification")
                  .Items(children =>
                  {
                      children.Add().Text("Email");
                  });

              items.Add()
                  .Text("Theme");

          })
            )

有人知道我怎样在.Text("Account");之前添加一个font-awesome图标吗?
1个回答

7
这对我来说在一个示例项目中似乎有效。
如果您将.Text("Account")更改为以下内容:
 .Text("<span class=\"fa fa-arrow-up\"></span> Account").Encoded(false)

那么在“账户”旁边应该显示一个向上的箭头。 (显然,更改 Font Awesome 元素为所需元素。)

编辑:我已经为您添加了以下示例,显示此功能在多个级别上的工作并在子级别添加字体。

@(Html.Kendo()
      .Menu()
      .Name("men")
      .Items(item =>

                    {
                        item.Add()
                            .Text("<span class=\"glyphicons glyphicons-ok\"> </span>some item")
                            .Items(i =>
                                        {
                                            i.Add().Text("<span class=\"glyphicons glyphicons-plus\"></span> Hello").Encoded(false);
                                        }
                                  )
                            .Encoded(false);
                        item.Add()
                            .Text("<span class=\"glyphicons glyphicons-thumbs-up\"> </span>some item")
                            .Items(i => 
                                       { 
                                           i.Add().Text("Hello"); 
                                       })
                            .Encoded(false);
                    })
)

设置 .Encoded(false) 的原因是,渲染引擎仅传递数据并假定它是安全的代码以输出,这相当于执行以下操作: @Html.Raw("<p> some html here</p>") 将其设置为 true 后,系统仅将传入的文本视为字符串,并不尝试解释该文本,也不应用任何“HTML/JavaScript”识别,例如:<p>I'm a paragraph</p> 如果编码设置为 true,则渲染出 <p>I'm a paragraph</p>,如果为 false,则会将I'm a paragraph作为自己的段落,并将标记应用到页面上。

似乎不喜欢 .EncodeVisual Studio 说“无法解析符号'Encode'”。我将其删除,但现在它只显示 .Text("") 中的代码,而不是 Font Awesome 图标。 - Justin
改为 encoded,完美解决!谢谢! - Justin
我有两个快速问题,.encoded的目的是什么?另外,当我在通知中添加.encoded时,它会在.items和.add上出错。它似乎不喜欢菜单中的子项。 - Justin
1
我为您添加了一些示例代码供您测试(我使用的是glyphicon而不是Font Awesome,因此根据需要进行替换)。这是一个快速的概念验证代码,并已经过测试,以确保字形显示正确。 - David Shorthose

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接