简体   繁体   English

Blazor 应用程序中的动态自定义元素

[英]Blazor Dynamic Custom Elements in application

I'm developing a webapp built in C# with Blazor WASM that is Asp.Net hosted.我正在开发一个内置于 C# 和 Blazor WASM 的 webapp,它是 Asp.Net 托管的。 I'm making a blazor component that through the use of a library already in production, will generate a HTML fragment (or full embed) that is then displayed in this way我正在制作一个 blazor 组件,通过使用已经在生产中的库,将生成一个 HTML 片段(或完全嵌入),然后以这种方式显示

...
    <div>
        @((MarkupString)document)
    </div>
...

with document containing the markup generated by the library.带有包含库生成的标记的document

As long as we're doing it with static content all is fine and dandy, but now we need to have some input in there that will then be sent back to the server to execute some actions.只要我们使用 static 内容进行操作,一切都很好,但现在我们需要在其中输入一些内容,然后将其发送回服务器以执行一些操作。

In a MarkupString there is no way to include <InputFile /> or <InputText /> components in such a way that they are shown in the fragment and I can read their contents, and I can find no way to actually interact with the standard HTML tags, especially regarding the file upload.MarkupString中,无法以在片段中显示的方式包含<InputFile /><InputText />组件,我可以读取它们的内容,而且我找不到与标准 HTML 实际交互的方法标签,尤其是关于文件上传。

Moreover we'll probably soon need to have a specific image uploader with preview which would be a custom Blazor component and this led me to the CustomElements .NET 7 feature that looks like what I need for both problems.此外,我们可能很快就需要一个带有预览的特定图像上传器,它是一个自定义的 Blazor 组件,这让我想到了 CustomElements .NET 7 功能,它看起来像我需要的两个问题。

However I couldn't find how to actually implement this in my app, and the documentation I found is still very partial in that way.但是我找不到如何在我的应用程序中实际实现它,而且我找到的文档在这方面仍然非常片面。 Is there a way to do what I need?有没有办法做我需要的?

EDIT: Managed to fix this partially, with Chen's answer.编辑:通过 Chen 的回答设法部分解决了这个问题。 I still have trouble with the binding though, as the @bind-Value directive is not working with不过,我在绑定方面仍然遇到问题,因为@bind-Value指令不适用于

Unhandled exception rendering component: Microsoft.AspNetCore.Components.Forms.InputText requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'.

with this markup:使用此标记:

...
<custom-input-text @bind-value="$field1" name="$field1"></custom-input-text>
...

(the capital V in bind-Value becomes lowercase all by itself) (bind-Value 中的大写 V 全部变成小写)

Am I doing something wrong again?我是不是又做错了什么?

CustomElements should meet your requirements, you can create your own logic in Blazor components, and then use it in your application. CustomElements应该满足您的要求,您可以在 Blazor 组件中创建自己的逻辑,然后在您的应用程序中使用它。

To use the component, you need to add the following JavaScript script references to your host app in this specific order.要使用该组件,您需要按此特定顺序将以下 JavaScript 脚本引用添加到您的主机应用程序。

<script src="_content/Microsoft.AspNetCore.Components.CustomElements/BlazorCustomElements.js"></script>
<script src="_framework/blazor.webassembly.js"></script>

You also need to add the corresponding middleware:还需要添加相应的中间件:

app.UseBlazorFrameworkFiles();

And use app.UseWebAssemblyDebugging();并使用app.UseWebAssemblyDebugging(); for debugging.用于调试。

Then you need to register the corresponding component in the Blazor program:然后需要在Blazor程序中注册相应的组件:

builder.RootComponents.RegisterCustomElement<Counter>("my-counter");

Then you can call this component in your application, including passing parameters, etc.然后你就可以在你的应用中调用这个组件了,包括传递参数等等。

<my-counter title="Khalid" increment-amount="2" />

Here is a complete example with detailed explanation , you can use it as a reference.这里有一个 完整的示例,并有详细的解释,您可以将其作为参考。

Helpful links:有用的网址:

Blazor Custom Elements . Blazor 自定义元素

Using .NET 7's Blazor Custom Elements to render dynamic content .使用 .NET 7 的 Blazor 自定义元素呈现动态内容

ASP.NET Core Razor components . ASP.NET 核心 Razor 组件

Hope this can help you.希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM