Implementing an Interface in Blazor without Code-Behind
Your Blazor Components can implement interfaces even if you’re not using code-behind files. Given that a basic components looks like the following:
@page "/" <h1>Hello, world!</h1> Welcome to your new app. @functions { }
It’s not directly obvious how the implementation happens. But for that, we have the “@implements” directive:
@page "/" @implements IMainPage
We can do the actual implementation in the functions-block:
@functions { public void UpdateText() { // Implementation logic } }
Here’s a full example:
@page "/" @implements IMainPage <h1>Hello, world!</h1> Welcome to your new app. @functions { public void UpdateText() { // Implementation logic } }