@inherits LayoutComponentBase @implements IDisposable @inject NavigatorService NavigatorService @inject NavigationManager NavigationManager @inject IJSRuntime JS @inject State State
@Body
@code { private DotNetObjectReference? DotNetRef { get; set; } protected override void OnAfterRender(bool firstRender) { base.OnAfterRender(firstRender); if (firstRender) { // See warning about memory above in the article DotNetRef = DotNetObjectReference.Create(State); JS.InvokeVoidAsync("injectCSharp", DotNetRef); } } protected override void OnInitialized() { base.OnInitialized(); NavigatorService.NavigationManager = NavigationManager; State.IntentReceived += IntentRecieved; if (!string.IsNullOrEmpty(State.ShareString) || !string.IsNullOrEmpty(State.SharePhoto)) { IntentRecieved(); } } private void IntentRecieved(object? sender = null, EventArgs? e = null) { if (!string.IsNullOrEmpty(State.ShareString)) { NavigationManager.NavigateTo($"/sharetext/{State.ShareString}"); State.ShareString = null; } else if (!string.IsNullOrEmpty(State.SharePhoto)) { NavigationManager.NavigateTo($"/sharepic/{State.SharePhoto}"); State.SharePhoto = null; } } void IDisposable.Dispose() { State.IntentReceived -= IntentRecieved; DotNetRef?.Dispose(); } }