[AspNetCore 3.0] 在RazorPages/MVC 中使用 Blazor (Razor组件)

开发环境

Vs2019 16.3.1 dotnetcore 3.0css

1、开始

  1. 新建webapp项目
    dotnet new webapp -o projectname
    或Vs 中新建项目选择 Web应用程序。
    在StartUp.cs 中增长两处配置。
    ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();//启用服务端blazor支持
        }

Configure 方法html

app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapBlazorHub();// js,singalr 
            });
  1. 使用Vs打开项目,默认页面和目录暂时不动, 新增文件夹 RazorComponentsvue

  2. 在项目根目录下,右键菜单添加-新建项-Razor组件,命名_Imports.razor,用于导入razor组件所需命名空间(做用相似mvc中的_ViewImports.cshtml)。
    此文件对同级或子级文件夹中的*.razor生效, 将内容替换为
    @using Microsoft.AspNetCore.Components @using Microsoft.AspNetCore.Components.Web;git

  3. 在RazorComponents 文件夹上 右键菜单添加-新建项-Razor组件,命名CounterButton.razorgithub

2、 组件 CounterButton.razor

说明:web

  1. 呈现为html button 元素 ,显示当前计数。
  2. 用户单击按钮时回传给服务端,将计数+1,随后更新客户端文本。
  3. CounterButton.razor
    .razor文件本质为一个继承ComponentBase,名为CounterButton的c#类(全名为项目名称.文件夹.CounterButton)。
    打开CounterButton.razor 文件能够看到,@code指令(预览6以前的@functions)将文件分为两部分,上部为html标签,下部即为CounterButton类的实现部分。
    CounterButton.razorc#

    <h3>Component</h3>
    @code {//可脑补为 public class CounterButton:ComponentBase{
    //c#代码,属性、方法。。
    }
  4. 处理c#代码:
    增长属性 Count,增长方法 Click
    [Parameter]// 用于传递参数 public int Count { get; set; } void Click() { Count++; }
  5. 处理Html标记, CounterButton.razor 内容以下
    <button @onclick="Click" > Count:@Count </button> @code { [Parameter] public int Count { get; set; } void Click() { Count++; } }
    此时组件代码已完成,接下来转到Pages目录下,处理.cshtml浏览器

3、在.cshtml中使用

  1. 打开Pages/Index.cshtml,在你想要显示组件的地方插入代码
    @(await Html.RenderComponentAsync<RazorComponents.CounterButton>(RenderMode.Server))mvc

    • RenderMode 说明
    • 若是在Pages/_ViewImports.cshtml 加入using projectname.RazorComponents 调用以下
      @(await Html.RenderComponentAsync<CounterButton>(RenderMode.Server))
  2. 打开Pages/Shared/_Layout.cshtml, 加入
    <script src="_framework/blazor.server.js"></script>
    • 保证此脚本在组件render 位置以后加入
  3. 启动项目,打开浏览器,点击 button 查看效果。
    • 打开浏览器调试窗口-networks选项卡,其中 以_blazor开头的即为组件使用的signalR链接

4、使用组件参数

在以前的组件代码中有这样一行
[Parameter]// 用于传递参数
  public int Count { get; set; }

能够用来设置初始化参数,若是在另外一个.razor 文件中,咱们能够这样设置 Count的初始值
<CounterButton Count="2" />
可是,使用Html.RenderComponentAsync 时, RenderMode 为Server或ServerPrerendered 不支持参数。RenderMode.Static 仅输出静态Html(没法与服务端交互)。app

在目前阶段,咱们可使用一个无参数的razor组件过渡一下。

  1. 在项目中新增razor组件 ‘RazorPanel.razor’,为了演示,将此组件加到项目根目录下。
  2. 代码以下
//根据业务,将须要组合的razor组件放在一个组件内,能够方便的处理参数及组件间的关系
     <CounterButton Count="3"/>// 在_Imports.razor 中加入@using projectname.RazorComponents 或使用全名<projectname.RazorComponents.CounterButton             Count="3"/>
  1. 修改组件调用
    @(await Html.RenderComponentAsync<RazorPanel>(RenderMode.Server))

五 直接继承ComponentBase 实现组件

前面说过,组件是继承 ComponentBase的,所以能够用一个c#代码文件实现组件,如下以Icon为例。

  • 此Icon组件使用svg use方式,对应的js 定义来自[iconfont.cn]
  1. 新建组件 'Icon.razor'.
  2. 新建c#类 'Icon.razor.cs'.
public class IconBase: Microsoft.AspNetCore.Components.ComponentBase
    {
        [Parameter]
        public string IconName { get; set; } = "icon-user";
        [Parameter]
        public string IconClass { get; set; } = "icon";
    }
  • 文件名能够随意,使用*.razor.cs 格式 vs会帮你将.cs和对应的.razor组织在一块儿。
  1. 打开 Icon.razor,清除自动生成的内容,在第一行加入 @inherits IconBase
    完整代码
@inherits IconBase
<svg class="@IconClass" aria-hidden="true">
    <use  href="#@IconName"></use>
</svg>
<style scoped>
    .icon {
        height: 1em;
        /* 经过设置 font-size 来改变图标大小 */
        width: 1em;
        /* 图标和文字相邻时,垂直对齐 */
        vertical-align: -0.15em;
        /* 经过设置 color 来改变 SVG 的颜色/fill */
        fill: currentColor;
        /* path 和 stroke 溢出 viewBox 部分在 IE 下会显示
            normalize.css 中也包含这行 */
        overflow: hidden;
    }
</style>
  • 目前.razor 自动生成的类 为 class Icon 而不是 partial class Icon ,所以.cs 文件里的类名不能是Icon。也只能经过 @inherits 关联 .razor 里的html和c#代码。
  • .razor 看起来和vue组件有点相似
  • @if @foreach 等指令见文档
  • use标签中使用 href
  1. 在Pages/Shared/_Layout.cshtml 引入从[iconfont.cn]下载的js(一般为 iconfont.js);
  2. 在 RazorPanel.razor 中加入Icon :`

六 组件嵌套

razor组件上可使用ChildContent 属性嵌套其余组件,好比须要在CounterButton中加入一个Icon.

  1. 在CounterButton.razor 中增长一个属性
    [Parameter] public RenderFragment ChildContent { get; set; }

  2. 修改html 部分
    <button @onclick="Click"> @ChildContent Count:@Count </button>
  3. 打开RazorPanel.razor,修改 CounterButton 标记
    <CounterButton Count="3"> <ChildContent> <Icon IconName="icon-user" /> </ChildContent> </CounterButton>
    或 省略 <ChildContent>
    <CounterButton Count="3" > <Icon IconName="icon-user" /> </CounterButton>

七 组件引用

在想引用的子组件上定义 @ref ="组件引用名",在当前组件上定义同名字段捕获引用。
好比 在 RazorPanel.razor 中,给CounterButton 增长属性
<CounterButton Count="3" @ref="button" > <Icon IconName="icon-user" /> @button.Count </CounterButton>
@code
CounterButton button;//自动捕获 @ref="button" 的CounterButton 实例

其余

RenderFragment 委托,ComponentBase.BuildRenderTree 方法

  • 在cshtml 上渲染.razor 组件时,使用相似 RazorPanel 之类的容器来处理参数传递。
  • Microsoft.AspNetCore.Components.* 包括一些内置组件(Forms,Input*,Layout...)
  • 官方模板 dotnet new -i Microsoft.AspNetCore.Blazor.Templates
相关文章
相关标签/搜索