深圳网站建设 合作品牌,wordpress调用api接口,网站对联代码,聊城网站推广winform是一老技术#xff0c;感觉都有点掉牙了#xff08;我近20年前就是从winform开始接触.net的#xff09;#xff1b;blazor#xff0c;是微软技术圈里的新宠#xff0c;正在被悉心照顾。当这一老一少的技术碰撞后#xff0c;会有什么火花#xff1f;.net v6.0.0-… winform是一老技术感觉都有点掉牙了我近20年前就是从winform开始接触.net的blazor是微软技术圈里的新宠正在被悉心照顾。当这一老一少的技术碰撞后会有什么火花.net v6.0.0-preview.3给winform和blazor结合带来了前提。https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/Blazor/BlazorWinForm 是我写的一个简单的demo在winform窗体中引入blazor。先看一下长什么样是一个简单的用助记码查询药品的例子输入框查询按钮table的样式都是Bootstrap5.0的外层是一个winform的Form窗体。具体实现先看项目文件csproj的不一样Sdk要换成Microsoft.NET.Sdk.Razor然后是要通过nuget引入这些包。Project SdkMicrosoft.NET.Sdk.RazorPropertyGroupOutputTypeWinExe/OutputTypeTargetFrameworknet6.0-windows/TargetFrameworkUseWindowsFormstrue/UseWindowsForms/PropertyGroupItemGroupPackageReference IncludeDapper Version2.0.78 /PackageReference IncludeMicrosoft.AspNetCore.Components.WebView.WindowsForms Version6.0.0-preview.3.21201.13 /PackageReference IncludeMicrosoft.Extensions.DependencyInjection Version6.0.0-preview.3.21201.4 /PackageReference IncludeMicrosoft.NET.Sdk.Razor Version5.0.0-preview.8.20414.8 /PackageReference IncludeSystem.Data.SqlClient Version4.8.2 //ItemGroupItemGroupContent Updatewwwroot\app.cssCopyToOutputDirectoryAlways/CopyToOutputDirectory/ContentContent Updatewwwroot\Query.razorCopyToOutputDirectoryAlways/CopyToOutputDirectory/ContentContent Updatewwwroot\css\bootstrap.min.cssCopyToOutputDirectoryAlways/CopyToOutputDirectory/ContentContent Updatewwwroot\index.htmlCopyToOutputDirectoryAlways/CopyToOutputDirectory/ContentContent Updatewwwroot\js\bootstrap.min.jsCopyToOutputDirectoryAlways/CopyToOutputDirectory/Content/ItemGroupItemGroupNone Updatewwwroot\app.cssCopyToOutputDirectoryPreserveNewest/CopyToOutputDirectory/NoneNone Updatewwwroot\Query.razorCopyToOutputDirectoryAlways/CopyToOutputDirectory/NoneNone Updatewwwroot\index.htmlCopyToOutputDirectoryAlways/CopyToOutputDirectory/None/ItemGroup
/Project
通过csproj也知道我是添加了wwwroot文件夹并添加了一些前端文件index.html是一个模板页引入一些css和js!DOCTYPE html
htmlheadmeta charsetutf-8 /meta nameviewport contentwidthdevice-width, initial-scale1.0, maximum-scale1.0, user-scalableno /titleBlazor app/titlebase href/ /link href{PROJECT NAME}.styles.css relstylesheet /link hrefapp.css relstylesheet /link hrefcss/bootstrap.min.css relstylesheet /
/headbodydiv idapp classcontainer/divdiv idblazor-error-uiAn unhandled error has occurred.a href classreloadReload/aa classdismiss????/a/divscript src_framework/blazor.webview.js/scriptscript srcjs/bootstrap.min.js/script
/body
/html
css和js就不说了是前端的文件重点看query.razor这里实现了主要业务逻辑业务数据UI表现using Microsoft.AspNetCore.Components.Web
using Dapper
using System.Data.SqlClient;
div classrowdiv classcol-1/divdiv classcol-10div classinput-group mb-3input typetext classform-control idzjm placeholder请输入助记码 bindZJM aria-describedbybutton-addon2button classbtn btn-outline-secondary typebutton onclickGetGoods idbutton-addon2查询/button/div/divdiv classcol-1/div
/div
code {private string ZJM { get; set; }ListGoods list new ListGoods();void GetGoods(){using (var con new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[sqlcon].ConnectionString)){if (string.IsNullOrWhiteSpace(ZJM)){list con.QueryGoods(select top 30 spid,spmch,shpchd,shpgg from spkfk ).ToList();}else{list con.QueryGoods(select top 30 spid,spmch,shpchd,shpgg from spkfk where zjm like zjm ,new {zjm%ZJM% }).ToList();}}}
}
div classrowtable classtable table-striped table-hovertheadtr classtable-darkth scopecol编号/thth scopecol名称/thth scopecol产地/thth scopecol规格/th/tr/theadtbodyforeach (var item in list){trtditem.spid/tdtditem.spmch/tdtditem.shpchd/tdtditem.shpgg/td/tr}/tbody/table
/div
怎么引入到form窗体中呢这里有点像之前的winform中引入一个webview这里换成了BlazorWebView。using BlazorWinForm.wwwroot;
using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;
using System.Windows.Forms;namespace BlazorWinForm
{public partial class frmMain : Form{public frmMain(){InitializeComponent();var serviceCollection new ServiceCollection();serviceCollection.AddBlazorWebView();var blazor new BlazorWebView(){Dock DockStyle.Fill,HostPage wwwroot/index.html,Services serviceCollection.BuildServiceProvider(), };blazor.AutoScroll false;blazor.RootComponents.AddQuery(#app);Controls.Add(blazor);}}class Goods{public string spid { get; set; }public string spmch { get; set; }public string shpchd { get; set; }public string shpgg { get; set; }}
}
总体下来这种把winformc#html,css,js混合起来编程即把丰富的前端框架引入进来让winform表现更加灵活强大也不丢失cs架构对当前电脑的控制力(有很多行业是通过exe来对接专有设备的驱动的)。winform的UI之前是通过三方UI控件比如DevExpress来增强的虽然有WPF但还是在微软自己画的一个圈圈里转blazor则是带来了丰富的前端库。另外与之前winform嵌入webview不一样的是blazor中的c#可以方便的和winform来互动语言相通直接交流比如你可以很轻松的在blazor的页里里new一个form出来。Blazor进winform是鸡肋还是革新应该是仁者见仁智者见智了。