我对blazor-server-side有一些澄清。 blazor native和wrapper组件有什么区别?谁能帮帮我吗?
免责声明:我没有使用blazor,但是从第一次演示开始就跟着它一点点,但我从来没有听说过原生和包装组件nor does the documentation include this wording。因此我可能错了,但从我的想法得出的结论是,通过“包装器”实现互操作性。
我将引用this excelent blog post的一些文字
可能是原生的,因为组件仅由.NET bl(r)azor创建,没有javascript:
由于Razor Components将服务器端作为.NET Standard应用程序运行,因此逻辑是使用.NET技术编写的。这是可能的,因为Blazor框架采用了RenderTree,这是一种类似于流行的JavaScript框架(如Angular和React)中使用的虚拟DOM的DOM抽象。让我们看一下框架的UI方面,以了解组件的编写方式。
<p>Current count: @currentCount</p>
<button class="btn btn-primary" onclick="@IncrementCount">
Click me
</button>
@functions {
int currentCount = 0;
[Parameter] protected int CountBy { get; set; } = 1;
void IncrementCount()
{
currentCount += CountBy;
}
}
可能是包装器,因为我们在我们的组件中使用了JS功能的互操作层。
此外,Razor Components应用程序可以使用JavaScript生态系统的依赖关系,通过互操作性层,应用程序可以与.NET和JavaScript依赖关系进行双向通信。这对于Razor组件不支持必要的浏览器/ DOM API或现有JavaScript库很有用的情况很有用。
GeoLocation.cs(.NET)
public class Geolocation
{
// ...
public async Task GetCurrentPosition(
Action<Position> onSuccess,
Action<PositionError> onError,
PositionOptions options = null)
{
OnGetPosition = onSuccess;
OnGetPositionError = onError;
await JSRuntime.Current.InvokeAsync<bool>(
"interopGeolocation.getCurrentPosition",
new DotNetObjectRef(this),
options);
}
// ...
}
interopGeolocation.js(浏览器)
window.interopGeolocation = {
getCurrentPosition: function (geolocationRef, options) {
const success = (result) => {
geolocationRef.invokeMethodAsync(
'RaiseOnGetPosition',
interopGeolocation.toSerializeable(result));
};
const error = (er) =>
geolocationRef.invokeMethodAsync(
'RaiseOnGetPositionError',
er.code);
navigator.geolocation.getCurrentPosition(
success,
error,
options);
},
// ...
希望这可以帮助...