广州华隆酒店,2015解放军裁军方案,劈腿什么意思
vs2019 16.3.1 dotnetcore 3.0
dotnet new webapp -o projectname
public void configureservices(iservicecollection services) { services.addrazorpages(); services.addserversideblazor();//启用服务端blazor支持 }
configure 方法
app.useendpoints(endpoints => { endpoints.maprazorpages(); endpoints.mapblazorhub();// js,singalr });
使用vs打开项目,默认页面和目录暂时不动, 新增文件夹 razorcomponents
在项目根目录下,右键菜单添加-新建项-razor组件,命名_imports.razor
,用于导入razor组件所需命名空间(作用类似mvc中的_viewimports.cshtml)。
此文件对同级或子级文件夹中的*.razor生效, 将内容替换为
@using microsoft.aspnetcore.components @using microsoft.aspnetcore.components.web;
在razorcomponents 文件夹上 右键菜单添加-新建项-razor组件,命名counterbutton.razor
。
说明:
counterbutton.razor
.razor文件本质为一个继承componentbase,名为counterbutton的c#类(全名为项目名称.文件夹.counterbutton)。
打开counterbutton.razor 文件可以看到,@code指令(预览6之前的@functions)将文件分为两部分,上部为html标签,下部即为counterbutton类的实现部分。
counterbutton.razor
<h3>component</h3> @code {//可脑补为 public class counterbutton:componentbase{ //c#代码,属性、方法。。 }
[parameter]// 用于传递参数 public int count { get; set; } void click() { count++; }
处理html标记, counterbutton.razor 内容如下
<button @onclick="click" > count:@count </button> @code { [parameter] public int count { get; set; } void click() { count++; } }
此时组件代码已完成,接下来转到pages目录下,处理.cshtml
打开pages/index.cshtml,在你想要显示组件的地方插入代码
@(await html.rendercomponentasync<razorcomponents.counterbutton>(rendermode.server))
@(await html.rendercomponentasync<counterbutton>(rendermode.server))
<script src="_framework/blazor.server.js"></script>
在之前的组件代码中有这样一行
[parameter]// 用于传递参数 public int count { get; set; }
可以用来设置初始化参数,如果在另一个.razor 文件中,我们可以这样设置 count的初始值
<counterbutton count="2" />
但是,使用html.rendercomponentasync 时, rendermode 为server或serverprerendered 不支持参数。rendermode.static 仅输出静态html(无法与服务端交互)。
在目前阶段,我们可以使用一个无参数的razor组件过渡一下。
//根据业务,将需要组合的razor组件放在一个组件内,可以方便的处理参数及组件间的关系 <counterbutton count="3"/>// 在_imports.razor 中加入@using projectname.razorcomponents 或使用全名<projectname.razorcomponents.counterbutton count="3"/>
@(await html.rendercomponentasync<razorpanel>(rendermode.server))
前面说过,组件是继承 componentbase的,因此可以用一个c#代码文件实现组件,以下以icon为例。
public class iconbase: microsoft.aspnetcore.components.componentbase { [parameter] public string iconname { get; set; } = "icon-user"; [parameter] public string iconclass { get; set; } = "icon"; }
@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组件上可以使用childcontent 属性嵌套其他组件,比如需要在counterbutton中加入一个icon.
在counterbutton.razor 中增加一个属性
[parameter] public renderfragment childcontent { get; set; }
<button @onclick="click"> @childcontent count:@count </button>
打开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 方法
dotnet new -i microsoft.aspnetcore.blazor.templates
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论