title
stringlengths
3
46
content
stringlengths
0
1.6k
19:320
Customer myCustomer = ...
19:321
...
19:322
<div>
19:323
...
19:324
@myRenderFragment(myCustomer)
19:325
...
19:326
</div>
19:327
...
19:328
19:329
Being functions, render fragments can be passed to component parameters like all other types. However, Blazor has a specific syntax to make it easier to simultaneously define and pass render fragments to components: the template syntax. First, you define the parameters in your component:
19:330
[Parameter]
19:331
Public RenderFragment<Customer>CustomerTemplate {get; set;}
19:332
[Parameter]
19:333
Public RenderFragment Title {get; set;}
19:334
19:335
Then, when you call the customer, you can do the following:
19:336
<Detail>
19:337
<Title>
19:338
<h5>This is a title</h5>
19:339
</Title>
19:340
<CustomerTemplate Context=customer>
19:341
<p>Customer name is @customer.Name.</p>
19:342
</CustomerTemplate>
19:343
</Detail>
19:344
19:345
Each RenderFragment parameter is represented by a tag with the same name as the parameter. You can place the markup that defines RenderFragment inside of it. For the CustomerTemplate that has a parameter, the Context keyword defines the parameter name inside the markup. In our example, the chosen parameter name is customer.
19:346
When a component has just one render fragment parameter, if it is named ChildContent, the template markup can be enclosed directly between the opening and closing tags of the component:
19:347
[Parameter]
19:348
Public RenderFragment<Customer> ChildContent {get; set;}
19:349
……………
19:350
……………
19:351
<IHaveJustOneRenderFragment Context=customer>
19:352
<p>Customer name is @customer.Name.</p>
19:353
</IHaveJustOneRenderFragment>
19:354
19:355
In order to familiarize ourselves with component templates, let’s modify the Pages->Weather.razor page so that, instead of using foreach, it uses a Repeater component.
19:356
Let’s right-click on the Layout folder, select Add and then Razor Component, and add a new Repeater.razor component. Then, replace the existing code with this:
19:357
@typeparam T
19:358
@foreach(var item in Values)
19:359
{
19:360
@ChildContent(item)
19:361
}
19:362
@code {
19:363
[Parameter]
19:364
public RenderFragment<T> ChildContent { get; set; }
19:365
[Parameter]
19:366
public IEnumerable<T> Values { get; set; }
19:367
}
19:368
19:369
The component is defined with a generic parameter so that it can be used with any IEnumerable. Now let’s replace the markup in the tbody of the Weather.razor component with this:
19:370
<Repeater Values=`forecasts` Context=`forecast`>
19:371
<tr>
19:372
<td>@forecast.Date.ToShortDateString()</td>
19:373
<td>@forecast.TemperatureC</td>
19:374
<td>@forecast.TemperatureF</td>
19:375
<td>@forecast.Summary</td>
19:376
</tr>
19:377
</Repeater>
19:378
19:379
Since the Repeater component has just one template, and since we named it ChildContent, we can place our template markup directly within the component open and close tags. Run it and verify that the page works properly. You have learned how to use templates and that markup placed inside a component defines a template.
19:380
An important predefined templated Blazor component is the CascadingValue component. It renders the content placed inside of it with no changes, but passes a type instance to all its descendant components:
19:381
<CascadingValue Value=`new MyOptionsInstance{...}`>
19:382
……
19:383
</CascadingValue>
19:384
19:385
All components placed inside of the CascadingValue tag and all their descendant components can now capture the instance of MyOptionsInstance passed in the CascadingValue parameter. It is enough that the component declares a public or private property with a type that is compatible with MyOptionsInstance and that decorates it with the CascadingParameter attribute:
19:386
[CascadingParameter]
19:387
private MyOptionsInstance options {get; set;}
19:388
19:389
Matching is performed by type compatibility. If there’s ambiguity with other cascaded parameters with a compatible type, we can specify the Name optional parameter of the CascadingValue component and pass the same name to the CascadingParameter attribute: [CascadingParameter(`myUnique name`)].
19:390
The CascadingValue tag also has an IsFixed parameter that should be set to true whenever possible for performance reasons. In fact, propagating cascading values is very useful for passing options and settings, but it has a very high computational cost.
19:391
When IsFixed is set to true, propagation is performed just once, the first time that each piece of content involved is rendered, and then no attempt is made to update the cascaded value during the content’s lifetime. Thus, IsFixed can be used whenever the pointer of the cascaded object is not changed during the content’s lifetime.
19:392
An example of a cascading value is the CascadingAuthenticationState component we encountered in the Routing subsection, which cascades authentication and authorization information to all rendered components.
19:393
Error handling
19:394
As the default, when an error in a component occurs, the exception is intercepted by the .NET runtime, which automatically makes visible the error code contained in index.html:
19:395
<div id=`blazor-error-ui`>
19:396
An unhandled error has occurred.
19:397
<a href=`` class=`reload`>Reload</a>
19:398
<a class=`dismiss`> / </a>
19:399
</div>
19:400
19:401
However, component errors can be intercepted and handled locally by enclosing the component inside an ErrorBoundary component. Below, the code of the Repeater example in the previous subsection has been modified to locally handle errors that might occur in each row:
19:402
<Repeater Values=`forecasts` Context=`forecast`>
19:403
<tr>
19:404
<ErrorBoundary>
19:405
<ChildContent>
19:406
<td>@forecast.Date.ToShortDateString()</td>
19:407
<td>@forecast.TemperatureC</td>
19:408
<td>@forecast.TemperatureF</td>
19:409
<td>@forecast.Summary</td>
19:410
</ChildContent>
19:411
<ErrorContent>
19:412
<td colspan=`4` class=`my-error`>Nothing to see here. Sorry!</td>
19:413
</ErrorContent>
19:414
</ErrorBoundary>
19:415
</tr>
19:416
</Repeater>
19:417
19:418
The standard code is placed in the ChildContent template, while the ErrorContent template is shown if there’s an error.
19:419
Events