0

I'm trying to convert some lines of Razor code to C# code but can't seem to fix it.I got the following Razor code (the layout isn't important here so I just leave that out).

    @page "/batch"
    @layout ApmStandardLayout
    @inherits TestApp.Pages.BatchComponent
    @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 just copy pasting this into a C# editor (and removing all the '@'), I'm still getting some errors. The following errors occur:

  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

I'm not an experienced C# programmer so help will be appreciated!

Thanks in advance.

hopeloos
  • 1
  • 2
  • What makes you think, you _can_ just copy&paste that and remove the "@"s and it'll work? And why are you doing that? – Fildor Feb 09 '21 at 10:16
  • @Fildor because Razor looks a lot like C#. I'm working with a low code platform called Radzen and I want to keep my layout page (.razor file) clean. So these lines of code need to move to the backend (.razor.cs file). I thought Razor and C# were almost the same but I guess that's my bad for not knowing. – hopeloos Feb 09 '21 at 10:23
  • Well, it _is_ embedded C# code. But moving it to the backend is not _that_ easy, I am afraid. – Fildor Feb 09 '21 at 10:24
  • @Fildor So there is a way to do this? Do you have a link to a guide etc.? – hopeloos Feb 09 '21 at 10:29
  • 1
    In this case, I am actually not sure, what exactly you want to shift to the backend. It seems as if there is only a SignalR handler that updates your UI. That doesn't make too much sense on the backend. – Fildor Feb 09 '21 at 10:33
  • I guess he misspelled, the hint from him `.razor.cs` intends to me he want's to move it to the code behind of the razor file. See here https://stackoverflow.com/questions/56111322/how-to-create-code-behind-blazor-components-with-vs-2019/59470941#59470941 – Maximilian Ast Feb 09 '21 at 10:42
  • 1
    @MaximilianAst Ah yes, I meant the behind Razor file indeed. My bad. I will look at the sites you sent me. Thanks! – hopeloos Feb 09 '21 at 10:48
  • _"he want's to move it to the code behind of the razor file"_ - Ahhh - whole different story. Ok. – Fildor Feb 09 '21 at 10:51

0 Answers0