title
stringlengths
3
46
content
stringlengths
0
1.6k
19:220
From version 6, parameters can also be extracted from the query string: /orderitem/{customer:int}?order=123.
19:221
@namespace overrides the default namespace of the component, while @using is equivalent to the usual C# using. The @using declared in the special {project folder}->_Imports.razor folder is automatically applied to all components.
19:222
@inherits declares that the component is a subclass of another component, while @implements declares that it implements an interface.
19:223
@typeparam is used if the component is a generic class and declares the name of the generic parameter. @typeparam also allows the specification of generic constraints with the same syntax used in classes: @typeparam T where T: class.
19:224
@attribute declares any attribute applied to the component class. Property-level attributes are applied directly to properties defined in the code area, so they don’t need special notation. The [Authorize] attribute, applied to a component class used as a page, prevents unauthorized users from accessing the page. It works exactly in the same way as when it is applied to a controller or to an action method in ASP.NET Core MVC.
19:225
Finally, the @inject directive requires a type instance to the dependency injection engine and inserts it in the field declared after the type name; in the previous example, in the navigation parameter.
19:226
19:227
The middle part of the component file contains the HTML that will be rendered by the component with Razor markup, enriched with the possible invocation of child components.
19:228
The bottom part of the file is enclosed by an @code construct and contains fields, properties, and methods of the class that implements the component:
19:229
@code{
19:230
...
19:231
private string myField=`0`;
19:232
[Parameter]
19:233
public int Quantity {get; set;}=0;
19:234
private void IncrementQuantity ()
19:235
{
19:236
Quantity++;
19:237
}
19:238
private void DecrementQuantity ()
19:239
{
19:240
Quantity--;
19:241
if (Quantity<0) Quantity=0;
19:242
}
19:243
...
19:244
}
19:245
19:246
Public properties decorated with the [Parameter] attribute work as component parameters; that is, when the component is instantiated into another component, they are used to pass values to the decorated properties, and those values are passed to HTML elements in HTML markup:
19:247
<OrderItem Quantity =`2` Id=`123`/>
19:248
19:249
Values can also be passed to component parameters by page route parameters, or query string parameters that match the property name in a case-invariant match:
19:250
OrderItem/{id}/{quantity}
19:251
OrderItem/{id}?quantity = 123
19:252
19:253
However, the match with query string parameters is enabled only if the property is also decorated with the SupplyParameterFromQuery attribute:
19:254
[Parameter]
19:255
[SupplyParameterFromQuery]
19:256
public int Quantity {get; set;}=0;
19:257
19:258
Component parameters can also accept complex types and functions:
19:259
<modal title='() => `Test title` ' ...../>
19:260
19:261
If components are generic, they must be passed type values for each generic parameter declared with typeparam:
19:262
<myGeneric T= `string`...../>
19:263
19:264
However, often, the compiler is able to infer generic types from the types of other parameters.
19:265
Finally, the code enclosed in the @code directive can also be declared in a partial class with the same name and namespace as the component:
19:266
public partial class Counter
19:267
{
19:268
[Parameter]
19:269
public int CurrentCounter {get; set;}=0;
19:270
...
19:271
...
19:272
}
19:273
19:274
Usually, these partial classes are declared in the same folder as the component and with a filename equal to the component’s filename with a .cs postfix added. Thus, for instance, the partial class associated with the counter.razor component will be counter.razor.cs.
19:275
Each component may also have an associated CSS file, whose name must be the name of the component file plus the .css postfix. Thus, for instance, the CSS file associated with the counter.razor component will be counter.razor.css. The CSS contained in this file is applied only to the component and has no effect on the remainder of the page. This is called CSS isolation, and at the moment, it is implemented by adding a unique attribute to all component HTML roots. Then, all selectors of the component CSS file are scoped to this attribute so that they can’t affect other HTML.
19:276
Of course, we can also use global application CSS, and in fact, the Blazor template creates the wwwroot/css/app.css file for this purpose.
19:277
When the Blazor application is packaged, either during a build or during its publication, all isolated CSS is processed and placed in a unique CSS file called <assembly name>.Client.styles.css. That’s why the index.html page of our BlazorReview application contains the following CSS reference:
19:278
<head>
19:279
...
19:280
<link href=`BlazorReview.Client.styles.css` rel=`stylesheet` />
19:281
</head>
19:282
19:283
It is worth mentioning that isolated CSS can also be obtained with pure CSS techniques, or using the Sass language, which compiles with CSS.
19:284
Whenever a component decorates an IDictionary<string, object> parameter with [Parameter(CaptureUnmatchedValues = true)], then all unmatched parameters inserted into the tag, that is, all parameters without a matching component property, are added to the IDictionary as key-value pairs.
19:285
This feature provides an easy way to forward parameters to HTML elements or ­other child components contained in the component markup. For instance, if we have a Detail component that displays a detail view of the object passed in its Value parameter, we can use this feature to forward all usual HTML attributes to the root HTML tag of the component, as shown in the following example:
19:286
<div @attributes=`AdditionalAttributes`>
19:287
...
19:288
</div>
19:289
@code{
19:290
[Parameter(CaptureUnmatchedValues = true)]
19:291
public Dictionary<string, object>
19:292
AdditionalAttributes { get; set; }
19:293
[Parameter]
19:294
Public T Value {get; set;}
19:295
}
19:296
19:297
This way, usual HTML attributes added to the component tag, for instance, class, are forwarded to the root div of the components and used to style the component:
19:298
<Detail Value=`myObject` class=`my-css-class`/>
19:299
19:300
The next subsection explains how to pass markup-generating functions to components.
19:301
Templates and cascading parameters
19:302
Blazor works by building a data structure called a render tree, which is updated as the UI changes. At each change, Blazor locates the part of the HTML that must be rendered and uses the information contained in the render tree to update it.
19:303
The RenderFragment delegate defines a function that is able to add further markup to a specific position of the render tree. There is also RenderFragment<T>, which accepts a further argument you can use to drive the markup generation. For instance, you can pass a Customer object to RenderFragment<T> so that it can render all the data for that specific customer.
19:304
You can define RenderFragment or RenderFragment<T> with C# code, but the simplest way is to define it in your components with Razor markup. The Razor compiler will take care of generating the proper C# code for you:
19:305
RenderFragment myRenderFragment = @<p>The time is @DateTime.Now.</p>;
19:306
RenderFragment<Customer> customerRenderFragment =
19:307
(item) => @<p>Customer name is @item.Name.</p>;
19:308
19:309
The information on the location to add the markup is passed in the RenderTreeBuilder argument it receives as an argument. You can use RenderFragment in your component Razor markup by simply invoking it as shown in the following example:
19:310
RenderFragment myRenderFragment = ...
19:311
...
19:312
<div>
19:313
...
19:314
@myRenderFragment
19:315
...
19:316
</div>
19:317
...
19:318
19:319
The position where you invoke RenderFragment defines the location where it will add its markup, since the component compiler is able to generate the right RenderTreeBuilder argument to pass to it. RenderFragment<T> delegates are invoked as shown here: