当前位置:网站首页>Blazor University (33) form - editcontext, fieldidentifiers
Blazor University (33) form - editcontext, fieldidentifiers
2022-06-26 02:19:00 【Dotnet cross platform】
Link to the original text :https://blazor-university.com/forms/editcontext-fieldidentifiers-and-fieldstate/
EditContext、FieldIdentifiers and FieldState
Please note that , For those who want to know Blazor how “ Backstage ” For those who work , This is a high-level theme . This information is not required for normal use Blazor - But I think it might be useful to know the inside information .
Let's start by describing Blazor How to maintain the meta state of form data UML Picture start .

EditContext
whenever EditForm.Model change ( The object being modified in the form ) when , It will be carried out EditForm.OnParametersSet And create a new EditContext example .EditForm Component will this EditContext Declared as a cascading value [1], So that any component in the form can access it .
EditContext Is the table cell data holder of the object currently being edited . Edit the object in the form ( for example Person) when ,Blazor Need to know more about this object , To provide a richer user experience .Blazor Other information held tells us :
If the specific properties of the model have been changed manually .
Which model properties have validation errors , What are these mistakes .
obviously , The model class being edited should only represent our specific business requirements , So having our model classes implement this extra UI state information would be a concern conflict —— therefore Blazor Store the extra information itself in EditContext in . This is it. EditForm In its Model Create a new EditContext Why , Because if Model change , be EditContext The information stored in is no longer relevant .
FieldIdentifier
FieldIdentifier The purpose of is to provide identification for specific attributes of an object . It is associated with System.Reflection.PropertyInfo Different , Because it identifies the properties of a particular object instance , The reflection identifies the properties of the class .
Given a with the name PostalCode Property of Address class , We can expect the following equality rules :
| value | Whether it is equal or not ? |
|---|---|
| // Reflection : Same properties on different instances | |
| address1.GetProperty(“ Postal Code ”); address2.GetProperty(“ Postal Code ”); | yes |
| // FieldIdentifier: The same attribute of the same instance | |
| new FieldIdentifier(address1, “PostalCode”); new FieldIdentifier(address1, “PostalCode”); | yes |
| // FieldIdentifier: Same properties on different instances | |
| new FieldIdentifier(address1, “PostalCode”); | |
| new FieldIdentifier(address2, “PostalCode”); | no |
When UI state ( For example, validation errors ) When an input value needs to be associated , We need some way to identify which input data this state is related to . In the past Web In technology , A string is usually used to identify a single input , Here are some examples :
EmailAddress
HomeAddress.PostalCode
WorkAddress.PostalCode
Once the user interface becomes complex , These paths can be very complex .
Contacts[0].Name
Contacts[0].ContactDetails[0].TelephoneNumber
Contacts[0].ContactDetails[0].EmailAddress
Contacts[9].ContactDetails[3].TelephoneNumber
Blazor Simplifies this process , Because its design allows it to always handle object and attribute identification in the same process . This enables us to identify any attribute of any object by storing two simple pieces of information .
1. Directly reference the object itself .2. The attribute name of the object .
The logo is now simple , also ( Different from string path ) Never have to change to take into account changes in data , For example, deleting items from an array . for example , In the string path method , If you want to delete the first contact in the list , You need to Contacts[9].ContactDetails[3].TelephoneNumber Change to Contacts[8].ContactDetails[3].TelephoneNumber.Blazor Of ObjectReference/PropertyName Method avoids this complexity .
Even if we write our own custom validator to talk to the server to determine validity ( For example, the availability of unique values , for example EmailAddress), our Blazor The verifier will also get a FieldIdentifier Example , You can then correlate the results from the server call and directly use the correct properties of the correct object instance .
FieldState
FieldState Class contains additional information about any object properties .EditContext Class has Dictionary<FieldIdentifier, FieldState> Private properties of type - This makes Blazor Its additional status can be stored in a flat list for quick access .
Given the following model
protected override OnInitialized()
{
var country1 = new Country
{
Code = "GB",
Name = "Great Britain"
};
var address1 = new Address
{
Line1 = "The Mansion",
Line2 = "Bletchley Park",
Line3 = "Sherwood Drive",
Town = "Bletchley",
Area = "Milton Keynes",
PostalCode = "MK3 6EB",
Country = country1
};
var person1 = new Person
{
Name = "My name",
Age = 12,
HomeAddress = address1,
TelephoneNumber = "+44 (0) 1908 64004"
};
}We want to see the following field identifiers :

Be careful : Entries are added to the dictionary only when needed .
边栏推荐
- 初识Opengl
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
- shell学习记录(四)
- Weishi camera display
- Agent challenge - "Olympic running"
- Connectez Le projecteur
- win32
- Sqlyog shortcut keys
- [image filtering] image filtering system based on Matlab GUI [including Matlab source code 1913]
- Analytic hierarchy process
猜你喜欢

Ardiuno smart mosquito racket

Cvpr2022 𞓜 future transformer with long-term action expectation

SDRAM控制器——仲裁模块的实现

ARM流水线如何提高代码执行效率

Visual studio 2013 redistributable is installed, but MySQL installation fails

微服务之consul

@Query difficult and miscellaneous diseases

CVPR2022 | 长期行动预期的Future Transformer

深度好文:什么是超网 Supernetting?

【缺陷检测】基于matlab GUI印刷电路板自动缺陷检测【含Matlab源码 1912期】
随机推荐
One minute to understand the difference between synchronous, asynchronous, blocking and non blocking
Shell learning record (IV)
基於鄰接矩陣的廣度優先遍曆
Connecting the projector
Create OpenGL window
【无标题】vsbiji esp....32
微服务之consul
Advanced cross platform application development (23): an article approaching the launch of testlight
Meaning of each state in TCP network communication
连接投影仪
螺旋矩阵
Output Lua print to the cocos2d console output window
基于邻接矩阵的深度优先遍历实现
Redis6.0 new feature - ACL (permission control list) implements the restriction of user executable commands and keys
静态库动态库的使用
Depth first traversal based on adjacency table
OA process editing
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
Convert Weishi camera pictures
ROS2+DDS+RTPS