Last active
April 14, 2022 13:28
-
-
Save LucGosso/d83f3eb85c1a81ecd8fd127ae4ddf402 to your computer and use it in GitHub Desktop.
Different razor component approaches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component type="typeof(Counter)" render-mode="ServerPrerendered" | |
param-Count="@Model.StartCount" /> | |
___above in cshtml________below in razor____________ | |
@using Microsoft.AspNetCore.Components | |
<p>Current count: @Count</p> | |
<button @onclick="IncrementCount" class="btn btn-primary">Click me</button> | |
<p>count: @Count</p> | |
@code { | |
[Parameter] | |
public int Count { get; set; } = 0; | |
private void IncrementCount() | |
{ | |
Count++; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component type="typeof(Counter)" render-mode="ServerPrerendered" | |
param-BlockID="@(Model as IContent).ContentLink" /> | |
___above in .razor________below in .cs____________ | |
@using EPiServer.Core | |
@using EPiServer | |
@using Epicweb.Mvc.Blazor.Models.Blocks | |
@using Microsoft.AspNetCore.Components | |
@inject IContentLoader ContentLoader | |
<h3>From Inside @CurrentBlock.StartCount</h3> | |
<p>Current count: @Count</p> | |
<button @onclick="IncrementCount" class="btn btn-primary">Click me</button> | |
@code { | |
public int Count { get; set; } = 0; | |
[Parameter] | |
public ContentReference BlockID { get; set; } | |
public CounterBlock CurrentBlock { get | |
{ | |
if (BlockID == null) | |
return null; | |
return ContentLoader.Get<CounterBlock>(BlockID); | |
} | |
} | |
protected override void OnInitialized() | |
{ | |
Count = CurrentBlock.StartCount; | |
base.OnInitialized(); | |
} | |
private void IncrementCount() | |
{ | |
Count++; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inherits CounterBase | |
<h3>From Inside @CurrentBlock.StartCount</h3> | |
<p>Current count: @Count</p> | |
<button @onclick="IncrementCount" class="btn btn-primary">Click me</button> | |
______Below counterbase.cs_____code separation_____________________ | |
using Epicweb.Mvc.Blazor.Models.Blocks; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.ServiceLocation; | |
using Microsoft.AspNetCore.Components; | |
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; | |
using System.Threading.Tasks; | |
namespace Epicweb.Mvc.Blazor.Views | |
{ | |
public partial class CounterBase: ComponentBase | |
{ | |
[Inject] | |
private ProtectedLocalStorage protectedLocalStorage { get; set; } | |
[Inject] | |
private IContentLoader contentLoader { get; set; } | |
public CounterBase() | |
{ | |
} | |
public CounterBase(ProtectedLocalStorage protectedLocalStorage, IContentLoader contentLoader) | |
{ | |
this.protectedLocalStorage = protectedLocalStorage; | |
this.contentLoader = contentLoader; | |
} | |
public int Count { get; set; } = 0; | |
[Parameter] | |
public ContentReference BlockID { get; set; } | |
public CounterBlock CurrentBlock | |
{ | |
get | |
{ | |
if (BlockID == null) | |
return null; | |
return contentLoader.Get<CounterBlock>(BlockID); | |
} | |
} | |
protected override void OnInitialized() | |
{ | |
Count = CurrentBlock.StartCount; | |
} | |
protected override async Task OnAfterRenderAsync(bool firstRender) | |
{ | |
if (firstRender) | |
{ | |
var result = await protectedLocalStorage.GetAsync<int>("Counter.Count" + BlockID.ID); | |
Count = result.Success ? result.Value : CurrentBlock.StartCount; | |
StateHasChanged(); | |
} | |
} | |
protected async Task IncrementCount() | |
{ | |
Count++; | |
await protectedLocalStorage.SetAsync("Counter.Count" + BlockID.ID, Count); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment