jeroentjeathome
jeroentjeathome

Reputation: 53

Intellisense does not recognize partial class

I am running a Blazor test project with Blazorized (which is awesome) and everything works fine. However, there is a small, irritating issue and that is that the code behind (.cs) of one of the (2) pages does not recognize that it is a partial class of a razor file enter image description here

enter image description here

So the protected override async Task OnInitializedAsync() says there is nothing to override. Code works fine however, but this is just bugging me. Tried rebuilding several times, quit Visual Studio and reload solution and also deleting the cache

Hope somebody has a clue...

enter image description here

Upvotes: 1

Views: 3058

Answers (2)

Preposterer
Preposterer

Reputation: 418

Take a look at your .csproj file to see if your partial class is being excluded from compile!

Sometimes Visual studio sometimes borks the csproj file when adding/removing .razor .cs files and you end up with unwanted statements removing your partial class .cs file from the project.

something like...

  <ItemGroup>
    <Compile Remove="Components\SomeComponent.razor.cs" />
  </ItemGroup>  

Happens all the time to me. Get rid of these unnecessary compile remove lines to get msbuild/intellisense to see your partial class .cs file.

Upvotes: 5

Vivek Nuna
Vivek Nuna

Reputation: 1

razor file and razor.cs class should have the same namespace. so if you try to change t default namespace in the razor.cs file, it will start giving you this error.

if you don't want to use the default namespace then you can define the same namespace in both files.

you can define the namespace in the razor file like this @namespace TestNameSpace on top. and in partial class like namespace TestNameSpace.

Typically, a component's namespace is derived from the app's root namespace and the component's location (folder) within the app. If the app's root namespace is BlazorSample and the Counter component resides in the Pages folder:

  • The Counter component's namespace is BlazorSample.Pages.
  • The fully qualified type name of the component is BlazorSample.Pages.Counter.

Reference: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-5.0#namespaces

Upvotes: 1

Related Questions