是否可以动态地将RCL (Razor Component Library)加载到Blazor WebAssembly?
我找到这个Loading an external .NET Standard 2.0 assembly with blazor来加载一个标准的类。
我想要的是开发一个可插拔/可扩展的可视化框架,其中将dll放在ASP.NET核心服务器文件夹中,在该文件夹中可以访问blazor组件
解决方案配置:
F 210
步骤:
打开Assembly
中动态使用
发布于 2022-03-29 20:34:45
我终于找到了一个我想和你分享的解决方案。模块管理器允许动态加载任何外部组件
看看这里:
https://github.com/elgransan/BlazorPluginComponents
一些示例代码
var componentPackage = "RazorClassLibrary2";
var component = "Component2";
var stream = await Http.GetStreamAsync($"{MyNavigationManager.BaseUri}/{componentPackage}/{componentPackage}.dll");
var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
componentType = assembly.GetType(componentPackage + "." + component);
await DOMinterop.IncludeLink(componentPackage, $"/{componentPackage}/{componentPackage}.styles.css");文件在服务器中的位置
发布于 2021-10-08 10:13:59
很抱歉迟了回答,但是的,你可以这么做。
要使用动态组件,请执行以下步骤:
使用Assembly.LoadfFrom(assemblyFilename)
RenderFragment EditContent = (__builder) =>
{
__builder.OpenComponent(0, TypeOfYourComponent);
__builder.AddAttribute(1, "attr1", attrValue);
...
__builder.AddAttribute(n, "attrn", attrNValue);
__builder.CloseComponent();
};
@EditContentvar exportedTypes = new List<Type>();
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
foreach (var assembly in assemblies)
{
exportedTypes.AddRange(assembly.GetExportedTypes().ToList());
}在我的示例中,我创建了一个在初始化时从文件夹加载程序集的单例,并在这个单例中创建了一个方法,从步骤3中定义的列表中返回类型。因此,通过在Startup.cs上注册它并将它注入您的剃刀组件,您可以在任何时候调用该服务:
public class CustomComponentService : ICustomComponentService
{
public CustomComponentService(...)
{
// load the assemblies here
}
public Type GetCustomComponent(...)
{
//search in the loaded assemblies by any criteria you like
}
}在Startup.cs ConfigureServices方法中:
_ = services.AddSingleton<ICustomComponentService, CustomComponentService>();在剃须刀档案中:
@inject ICustomComponentService CustomComponentService
...
__builder.OpenComponent(0, CustomComponentService.GetCustomComponent(...));
...https://stackoverflow.com/questions/65130285
复制相似问题