Make sure you installed the latest .NET Core 3.1.4 SDK.
All these samples runs on Blazor WebAssembly 3.2.0.
You will find samples for Blazor, a .NET application framework for Web Assembly here. To run the sample, simply type dotnet watch run
at the folder of each project. Make sure you pay attention which port Kestrel is running on.
-
The smallest Blazor app you can create.
-
This sample shows the simplest demonstration of a component (which is just a .cshtml file) that accept a string parameter. Notice that the folder name of the project is capitalized. That's due to this limitation e.g. the name of your folder is currently the name of the namespace used in a blazor app.
A component is represented by a
<componentname></componentname>
in the markup. -
Component Two - Reference and Call
This sample shows how to refer to a component using
ref
property and call a public method of the component. We use the methodthis.StateHasChanged();
to tell the component that its value has changed.Side note: If you are using
ref
without providing corresponding property atfunctions
, the compilation will fail. -
Component Three - Child Content
Implement a component that takes one or more child content e.g.
<parentcomponent><childcomponent1/><childcomponent2/><parentcomponent>
and renders it.You have to provide a parameter called
ChildContent
of typeRenderFragment
. The parameter name must beChildContent
otherwise it won't work. -
Component Four - Handling Custom Event from Component
This sample shows how to raise a custom event from a component and how to handle them using
EventCallback<>
. -
Component Five - Inherit from a ComponentBase class
This sample shows how to inherit from a
ComponentBase
class. This allows you to share common code across components. You can also use this technique to have a 'Code Behind' experience if that's your thing. -
Component Six - When to call StateHasChanged
This sample shows different behaviors by the component depending on where you call the method from. Note: this is still tentative. It needs more exploration.
-
This sample demonstrates that you can use and organize your C# classes like in an ordinary C# app.
-
Component Eight - Interactions between two components
This samples demonstrates the two way (currently) of facilitating interaction between two components.
-
Component Nine - Data binding from Child Component to Parent
This samples demonstrates another way on how to pass data from Child component to Parent using two way data binding. The other way is using Custom Event (see Component Four).
-
Component Ten - Data binding from Child Component to Parent on Collection
Similar to Component Nine except that this time the property is a
List<int>
instead of anint
. -
Component Eleven - Capture unmatched component parameters
Use
[Parameter(CaptureUnmatchedValues = true)]
to capture unmatched parameters. -
Component Twelve - Splatting arbitrary parameters to components
Use
@attributes
and aDictionary<string, object>
orList<KeyValuePair<string, object>>
. -
Component Thirteen - more example of attributes splatting
Use
@attributes
attributes splatting on an input form. -
Component Fourteen - various ways to pass data to components
This sample demonstrates the various ways to pass parameters to a component and how it affects on how the data is perceived by the component.
-
This sample demonstrates how to use partial class in a razor component. This allows you to separate your C# code from the markup.
-
Show an example of two day databinding to form element
input=text
,textarea
.input=checkbox
, andselect
. -
Show an example of
EditForm
and its 6 input controls, including form validation.