Moving @code{ } from .razor file to .razor.cs file

Greetings!

Maybe not really Radzen related but I hope you can help me out. I need to move some lines of code from a .razor file to a .razor.cs. file but I'm not sure on how to do that. I created the following code in the .razor file (note that the layout isn't important here so I just leave that out):

@page "/batch"
@layout ApmStandardLayout
@inherits TestApp.Pages.BatchComponent

@using Radzen
@using Radzen.Blazor
@using TestApp.Models.HmiDb
@using Microsoft.AspNetCore.Identity;
@using TestApp.Models
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
@using System.Threading
@using System.Threading.Tasks;
@using Microsoft.AspNetCore.SignalR.Client;

@inject NavigationManager NavigationManager    

@code { private TagData.TagData tagData = new TagData.TagData();

    private HubConnection hubConnection;

    protected override async Task OnInitializedAsync()
    {

        hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/tagservice"))
            .Build();

        hubConnection.On<TagData.TagData>("TagData", (data) =>
        {
            tagData = data;
            plctext_BatchID = data.plctext_BatchID.ToString("#.00");
            plctext_Data_CNC3_Products_Actual = data.plctext_Data_CNC3_Products_Actual.ToString("#.00");
            plctext_Data_CNC3_Destination_Actual = data.plctext_Data_CNC3_Destination_Actual.ToString("#.00");
            plctext_Data_CNC4_Products_Actual = data.plctext_Data_CNC4_Products_Actual.ToString("#.00");
            plctext_Data_CNC4_Destination_Actual = data.plctext_Data_CNC3_Destination_Actual.ToString("#.00");
            plctext_ProgressPercentage = data.plctext_ProgressPercentage.ToString("#.00");
            RecipeNameActual = data.RecipeNameActual;
            RecipeLengthActual = data.RecipeLengthActual.ToString(".00");
            RecipeDepthActual = data.RecipeDepthActual.ToString("#.00");
            RecipeBasketActual = data.RecipeBasketActual.ToString("#.00");

            StateHasChanged();
        });

        await hubConnection.StartAsync();

    } }

When I'm trying to move this to the .razor.cs file I'm getting two errors:

  1. Error CS0111 Type 'BatchComponent' already defines a member called 'OnInitializedAsync' with the same parameter types TestApp
  2. Error CS0103 The name 'NavigationManager' does not exist in the current context TestApp

The first error has something to do with OnInitializedAsync() being used already. I've found the code that uses this but I can't find a way around this to use the same function again.

        protected override async System.Threading.Tasks.Task OnInitializedAsync()
    {
        await Security.InitializeAsync(AuthenticationStateProvider);
        if (!Security.IsAuthenticated())
        {
            UriHelper.NavigateTo("Login", true);
        }
        else
        {
            await Load();
        }
    }

The second error is probably a wrong injection error. I've tried to convert @inject Navigationmanager NavigationManager to C# code but everything I do is wrong.

I'd hope any of you can help me with my problems because I really need to make these lines of code move to the .razor.cs file.

Thanks in advance!

Create another method in your *.razor.cs file and use Execute C# to invoke it in the Page Load event. Radzen pages already define OnInitializedAsync and you cannot specify a different implementation.

Radzen pages already have an instance of the NavigationManager class injected as the UriHelper property. You can use it instead. Otherwise declare a new property in the *.razor.cs file and decorate it with the Injectable attribute. More information is available in the Blazor documentation.

1 Like

Hello @korchev,

Thanks for the reply! Can you explain why the code I wrote in *.razor file does work with the NavigationManager but doesn't work in the *.razor.cs file? I'm trying to understand the difference :slight_smile:

Thanks!

The difference is explained in the Blazor documentation link that I have included - the syntax for injecting a property in a .razor file is different than a .razor.cs file.