42277

How to create “Code Behind” Blazor Components with VS 2019?

<h3>Question</h3>

I can create an inline component like

<pre class="lang-cs prettyprint-override">@foo

@functions { string foo = "foo"; }

However when I create Foo.razor containing just:

<pre class="lang-html prettyprint-override">

@foo

And Foo.razor.cs containing:

<pre class="lang-cs prettyprint-override">namespace MyApp.Client.Components { public class Foo: ComponentBase { public string foo; } }

I get:

Error CS0101 The namespace 'MyApp.Client.Components' already contains a definition for 'Foo'

I am using the latest VS 2019 and Blazor libraries.

What am I doing wrong?


<h3>Answer1:</h3>

Currently, the "code-behind" and .razor view can't share the same name.

So when you have Foo.razor.cs and Foo.razor it is seen as the same file and thus causes a collision.

Workaround for now: Rename your Foo.razor.cs to FooBase.cs (or something else).

Then in your Foo.razor, add @inherits FooBase

There is a GitHub issue regarding this here: https://github.com/aspnet/AspNetCore/issues/5487


<h3>Answer2:</h3>

Since October 2019, it is possible to use partial classes. So today you can name the class in the code-behind file like this:

public partial class Foo : ComponentBase { protected override Task OnInitializedAsync() { // do stuff } }

来源:https://stackoverflow.com/questions/56111322/how-to-create-code-behind-blazor-components-with-vs-2019

Recommend

  • Using one probability set to generate another [duplicate]
  • Creating and managing two independent random number sequences
  • CABasicAnimation creates empty default value copy of CALayer
  • Bazel failed to include a external static library .a
  • create circular Auto Horizontal Scroll View?
  • jQuery colorbox breaks postbacks in ASP.NET Web Forms
  • can you use embedded ruby in custom javascript files in rails?
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • How to move to lines with the same indentation in Visual Studio Code
  • Unable to connect to AWS RDS through PDO
  • PowerShell script to pass SecureString to Plink as account and sudo passwords
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • dmtracedump doesn't work, HELP!
  • Defer unused CSS
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • Implementation of timeout in LDAP
  • AWS RDS Parameter Group not changing MySQL encoding
  • Julia 1.0 UndefVarError - Scope of Variable
  • How to select multiple items from a List view - JavaFX 8
  • How to merge objects within array based on attribute
  • Floating parent div grows to hypothetical width of floating child div
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Terminal run dalvikvm with am.jar
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • reshape/remould data frame to create normalized bar chart and pie chart
  • Will this work on all screen sizes?
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to handle div that is created dynamically in a table
  • read part of h5 dataset python
  • Send array to next viewcontroller iOs xcode [duplicate]
  • ssh remote server login script
  • Make checkout phone field optional for specific countries in WooCommerce
  • Codeigniniter insert data through models and controller
  • How to call different template for different category archive page in woocommerce
  • PHP Permalinks.. how to change?
  • Spring Boot not autowiring @Repository
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ReferenceError: TextEncoder is not defined
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}
  • How to check if object is null in Java?