Using the Visual Studio for Mac I created a solution with name TestBlazorWebAssemblyProgressiveNetCoreApp
using the wizard and I selected the sample of WebAssembly of Blazor selecting "Hosted ASP.NET Core" and "Progressive web Application". Then I changed the NavMenu.razor
with this code:
@foreach (Item item in m_Items)
{
<div class="nav-item px-3">
<NavLink class="nav-link" href="@item.Path" Match="NavLinkMatch.All">
<span class="oi @item.TextStyle" aria-hidden="true"></span> @item.Title
</NavLink>
</div>
}
And I've added to this code to the @code
section:
public class Item
{
public ComponentBase Component { get; }
public string Path { get; }
public string Title { get; }
public string TextStyle { get; }
public Item(ComponentBase component, string path, string title, string textStyle)
{
Component = component;
Path = path;
Title = title;
TextStyle = textStyle;
}
}
private List<Item> m_Items = new List<Item> {
new Item(new TestBlazorWebAssemblyProgressiveNetCoreApp.Client.Pages.Index(), "", "Home", "oi-home"),
new Item(new TestBlazorWebAssemblyProgressiveNetCoreApp.Client.Pages.Counter(), "counter", "Counter", "oi-plus"),
};
public void AddItem(Item item)
{
m_Items.Add(item);
}
Then in the MainLayout.razor
, I added this code at the end:
@code {
private NavMenu NavMenuItem {
get
{
NavMenu navMenu = new NavMenu();
//navMenu.AddItem(new TestBlazorWebAssemblyProgressiveNetCoreApp.Client.Shared.NavMenu.Item(new TestBlazorWebAssemblyProgressiveNetCoreApp.Client.Pages.FetchData(), "fetchdata", "Fetch data", "oi-list-rich"));
return navMenu;
}
}
}
and I modify the html code commenting the <NavMenu>
and adding a call to @NavMenuItem
property, like this:
@NavMenuItem
<!--<NavMenu />-->
When I use the instance the menu doesn't appear (but the code of property NavMenuItem
is executed). And if I use the <NavMenu />
tag, the code works fine.
I would like to know what is wrong and if there are a way to add the instance of property to the razor page like a tag.
Thanks