Skip to main content

Getting Started with MudBlazor Hybrid

·588 words·3 mins· loading · loading ·
Benjamin Fehr
Author
Benjamin Fehr
Live and Learn: Discover, Evolve, Thrive

Getting Started with MudBlazor Hybrid: Creating a Custom Side Menu
#

In the world of Blazor development, creating a seamless user interface is a top priority. MudBlazor is a popular Material Design component library for Blazor that makes it easy to build beautiful and functional applications. In this blog post, we’ll explore how to start a new Blazor project using MudBlazor Hybrid and how to implement a custom side menu to enhance your application’s navigation.

What is MudBlazor?
#

MudBlazor is a component library for Blazor applications that follows Material Design principles. It provides a wide range of UI components that help developers build visually appealing and highly interactive interfaces. With MudBlazor Hybrid, you can create applications that run on both the web and mobile platforms.

Setting Up Your Project
#

Let’s jump into the steps to create a new MudBlazor Hybrid project.

Step 1: Install .NET SDK
#

Before you start, ensure you have the .NET SDK installed on your machine. You can download it from the .NET official website.

Step 2: Create a New Blazor Project
#

Open your terminal or command prompt and execute the following command:

dotnet new maui-blazor -n YourProjectName

This command will create a new MAUI Blazor project called YourProjectName.

Step 3: Add MudBlazor to Your Project
#

Navigate into your project folder:

cd YourProjectName

Next, you’ll need to add MudBlazor via NuGet. Run the following command:

dotnet add package MudBlazor

Once the package is added, you need to configure your application to use MudBlazor.

Step 4: Configure MudBlazor in Your Application
#

Open MainPage.xaml.cs and include the following using directive:

using MudBlazor.Services;

Update the MainPage constructor to include MudBlazor services:

public MainPage()
{
    InitializeComponent();
    // Add MudBlazor services
    DependencyService.Register<MudBlazor.Services.IMudBlazorService, MudBlazorService>();
}

In your App.razor file, include the MudBlazor components by adding the following lines:

<CascadingMudTheme>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there’s nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingMudTheme>

Creating a Custom Side Menu
#

Now that we have our project set up, let’s add a custom side menu using MudBlazor components.

Step 5: Create a Layout for Your Menu
#

Create a new Razor component called MainLayout.razor in the Shared folder. Here’s an example of how to implement a basic side menu layout:

@inherits LayoutComponentBase
<MudLayout>
    <MudDrawer @bind-Open="_drawerOpen" Clipped="true" Anchor="Start" Elevation="1">
        <MudDrawerHeader>Menu</MudDrawerHeader>
        <MudNavMenu>
            <MudNavLink Href="">Home</MudNavLink>
            <MudNavLink Href="about">About</MudNavLink>
            <MudNavLink Href="contact">Contact</MudNavLink>
        </MudNavMenu>
    </MudDrawer>
    <MudMainContent>
        @Body
    </MudMainContent>
</MudLayout>
@code {
    private bool _drawerOpen = false;
    protected override void OnInitialized()
    {
        // Optionally control drawer state
        _drawerOpen = true; // or false depending on your logic
    }
}

Step 6: Add Navigation Links#

In the MudNavMenu, feel free to expand it by adding links to your different pages. Make sure the pages referenced in the Href attributes correspond to your existing routes.

Step 7: Finalize Your Application
#

Run your application using:

dotnet run

You should see your custom side menu on the left side, allowing for easy navigation between the different pages of your application.

Conclusion
#

In this blog, we have walked through the steps to set up a new Blazor project with MudBlazor Hybrid and created a custom side menu for navigation. MudBlazor not only simplifies the development process but also enhances the aesthetic appeal of your application. Explore more of MudBlazor’s capabilities and customize your application further by diving into the documentation and experimenting with various components. Happy coding!

Additional
#

Related

HRI Expert, what is it and how to use it. Part 1
·3729 words·18 mins· loading · loading
HRI Expert Introduction # What is HRI® # HRI® stands for Hybrid Reactive Index.
Understanding Blazor Hybrid: Pros and Cons
·1423 words·7 mins· loading · loading
Introduction to Blazor Hybrid # Blazor Hybrid represents an innovative evolution in the world of web and desktop application development.
Calculate the most importand values from gear for work
·159 words·1 min· loading · loading
Interactive stock removal calculator # You can use the following interactive calculator to compute the stock removal per flanc.