Blazor
- SPA
- Single Page Application
- Load HTML and extra files only one time
- After first Loading, request by Signal(ex. AJAX) with no-reloading
- .cshtml and .razor
- .cshtml: Razor page file
- .razor: Razor component file
Route
-
@page
- define how to match urls
- method is same with Razor pages
Binding
- @
- specifies to use C#
- You can make attribute or condition by this, also
-
@key
- The @key directive attribute causes the components diffing algorithm to guarantee preservation of elements or components based on the key’s value.
-
@bind-value
- bind value by variable name
-
@code
- C# code in razor file
- seems like script in html file
Lambda
- ()=>
- is used for onclick method syntax
System.Threading
- System.Threading Namespace
- Provides classes and interfaces that enable multithreaded programming.
- In addition to classes for synchronizing thread activities and access to data (Mutex, Monitor, Interlocked, AutoResetEvent, and so on), this namespace includes a ThreadPool class that allows you to use a pool of system-supplied threads, and a Timer class that executes callback methods on thread pool threads.
- Timer Class
- Provides a mechanism for executing a method on a thread pool thread at specified intervals.
- This class cannot be inherited.
- InvokeAsync(Action)
- Executes the specified Action asynchronously on the thread the Dispatcher is associated with.
- StateHasChanged
- Notifies the component that its state has changed.
- When applicable, this will cause the component to be re-rendered.
Parameter
- In this post,
ShowUser.cshtml
is child andUser.cshtml
is parent - To use parent’s variable or function in child
- Parent
- link the variable with child’s parameter name
- Child
- define variable and spedify it is a parameter by
[Parameter]
- define variable and spedify it is a parameter by
ref
- In this post,
ShowUser.cshtml
is child andUser.cshtml
is parent - To use child’s variable or function in parent
- Parent
- define variable with child class name
EventCallBack
- EventCallback struckt
- A bound event handler delegate
- EventCallback.InvokeAsync Method
- Invokes the delegate associated with this binding and dispatches an event notification to the appropriate component.
- Diffence with
Action
- Prefer the strongly typed EventCallback
, which provides better error feedback to users of the component. - Similar to other UI event handlers, specifying the event parameter is optional. Use EventCallback when there’s no value passed to the callback.
- Prefer the strongly typed EventCallback
Cascading Parameter
- CascadingValue
- You can assign parameter for children
- be applied in children’s children
- parent
- define
Name
to match with child - specify variable in
Value
to use parameter
- define
- child
- link parameter by
Name
- link parameter by
Select Binding
- Select Binding
- bind
@bind
expression andvalue
attribute
- bind
Templated Component
- Templated Component
- Razor Component constructed with
RenderFragment
- Razor Component constructed with
- RenderFragment Delegate
- Represents a segment of UI content, implemented as a delegate that writes the content to a RenderTreeBuilder.
- Template
- You can use your parameters
- You can pass data with
Context
- Usage
- IReadOnlyList
Interface: Represents a read-only collection of elements that can be accessed by index. - You can make Generic Type in razor with
@typeparam
- IReadOnlyList
Partial
- Partial
- If you want to separate razor and code, make class file for code by same name with razor
- input
partial
betweenpublic
andclass
Dependency Injection
- Dependency
- In OOP, the lower the dependence, the better
- Rather than entering the class directly into the code, injecting dependencies using Startup.cs can reduce dependencies
- cs
- Create Interface
- Create Class that inherit the interface
- Startup.cs
- Start service in
ConfigureServices
- Start service in
- cshtml
- Inject the interface to use
- Difference with C++
- When you use same interfacein different class, you don’t need to service the interface again
- Asp.Net will connect it atomatically
Service lifetimes
Singleton
- Update every time server restart
Transient
- Update every time the page is requested
Scoped
- Update every time web connect
Form
Array -> List
- WeatherForecastService.cs
- To add new object, change Array to List
Modal
- Modal
- To use popup in page
- Use
if-else
like modal-switch - For UI, just use specified classes in Bootstrap
Validation
class
- [Required]
- Specify variable must be not null
- You can put error message in
ErrorMessage
- [Range]
- Specify range of variable
- You can define type in
typeof
, maximum, and minimum
- [StringLength]
- Specify length of string
- You can define length of maximum and mininum and put error message in
ErrorMessage
Razor Component
- EditForm
- To make Form
- Bind value in
Model
- Bind function in
OnValidSubmit
after pressing button that type issubmit
- DataAnnotationsValidator
- Add Data Annotations Validation
- ValidationSummary
- Show ErrorMessage
- InputNumber
- Make
<input>
tag with validation - Type is
int
- Bind value in
@bind-value
- Make
- InputText
- Make
<input>
tag with validation - Type is
string
- Bind value in
@bind-value
- Make
Action
- Action Delegate
- Encapsulates a method that has no parameters and does not return a value
- You can change UI State by Invoke()
Value
- value
- The value keyword is used to define the value being assigned by the set accessor.
IDisposable
- Idisposable Interface
- Provides a mechanism for releasing unmanaged resources
- Dispose()
- Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources
JavaScript
- Function
- To make a function, at first, you need to define variable
- define where you put your function. For example, window, document, and kind of tags
- define function’s name and contents like lambda function
- Call the function
- Script Binding
- You can link your script in
_Host.cshtml
by<sciprt>
tag
- You can link your script in
IJSRuntime
- IJSRuntime Interface
- Represents an instance of a JavaScript runtime to which calls may be dispatched
- InvokeAsync
- Invokes the specified JavaScript function asynchronously.
- Microsoft.JSInterop.JSRuntime will apply timeouts to this operation based on the value configured in Microsoft.JSInterop.JSRuntime.DefaultAsyncTimeout.
- To dispatch a call with a different timeout, or no timeout, consider using Microsoft.JSInterop.IJSRuntime.InvokeAsync
- An identifier for the function to invoke. For example, the value “someScope.someFunction” will invoke the function window.someScope.someFunction
- InvokeVoidAsync
- Invokes the specified JavaScript function asynchronously
- An identifier for the function to invoke. For example, the value “someScope.someFunction” will invoke the function window.someScope.someFunction