Skip to content

Instantly share code, notes, and snippets.

@shadow-cs
Created November 9, 2019 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadow-cs/b3a39769e249f3b116c1b4f41dcb2bda to your computer and use it in GitHub Desktop.
Save shadow-cs/b3a39769e249f3b116c1b4f41dcb2bda to your computer and use it in GitHub Desktop.
Blazor Code behind
namespace CodeBehindTest.Pages
{
public partial class Counter
{
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
}
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@shadow-cs
Copy link
Author

@danroth27 just watched Blazor intro from .NET Conf and figured out a second way to use code-behind which may be a little more familiar to people using XAML.

IMHO this is a little easier to use as you don't have to type in the ComponentBase base class and @inherits directive.

@danroth27
Copy link

Yep, support for partial classes was enabled in .NET Core 3.1 and will be the recommended pattern going forward. But if you're using .NET Core 3.0, then you'll need to use a base class due to issues with how the Razor tooling works.

@shadow-cs
Copy link
Author

Oh sure I did try that on 3.1 and VS Preview (just didn't have time to watch the videos fast enough), thanks for the heads up ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment