分享

C#+web=? 微软Blazor

 Mixlab交叉学科 2020-11-07
C#开发可以用Blazor在今后写前端渲染的网页了!
 

- C#(C sharp)是什么?
C#是微软公司发布的一种面向对象的、运行于.NET Framework和.NET Core(完全开源,跨平台)之上的高级程序设计语言。
 

- 为什么会出现Blazor?
我觉得起因是因为浏览器支持WebAssembly,WebAssembly是一种新的编码方式,可以在现代的网络浏览器中运行二进制格式文件,以接近原生的性能运行。Blazor 尝试使用WebAssembly和DotNetAnywhere将.NET带回到浏览器。
 
 
 
 
除了用C#来开发之外,还可以让C#运行在浏览器(使用WebAssembly)上,这样dotnet的众多api我们都可以在浏览器使用了。
 
 
 
Blazor文档齐全,
https://docs.microsoft.com/zh-cn/aspnet/core/blazor/get-started?view=aspnetcore-3.1&tabs=visual-studiohttps://docs.microsoft.com/zh-cn/aspnet/core/blazor/get-started?view=aspnetcore-3.1&tabs=visual-studio
 
按照官网指示,先安装 .NET Core 3.1 SDK。我使用的是mac环境,直接下载dotnet的pkg安装包安装。安装之后在终端输入,安装Blazor的模板:
dotnet new -i Microsoft.AspNetCore.Components.WebAssembly.Templates::3.2.0-preview2.20160.5

 
我使用的是 Visual Studio Code,安装C#插件
 
 
 
终端cd到合适的文件目录,输入:
dotnet new blazorwasm -o WebApplicationMix

用Visual Studio Code打开。终端输入:
dotnet run

浏览器打开https://localhost:5001/,初次体验Blazor
 
 
 
我比较看重还是Blazor可以调用dotnet提供的一个机器学习库。BlazorML5,为Blazor提供了ML.NET的功能。
 

- ML.NET

开源和跨平台的机器学习框架,它可以实现


情绪分析、产品推荐、价格预测、物体检测、图像分类等等。微软的api梳理得更为接近应用场景。代码风格也比较简洁:

//Step 1. Create a ML Contextvar ctx = new MLContext();
//Step 2. Read in the input data for model trainingIDataView dataReader = ctx.Data .LoadFromTextFile<MyInput>(dataPath, hasHeader: true);
//Step 3. Build your estimatorIEstimator<ITransformer> est = ctx.Transforms.Text .FeaturizeText("Features", nameof(SentimentIssue.Text)) .Append(ctx.BinaryClassification.Trainers .LbfgsLogisticRegression("Label", "Features"));
//Step 4. Train your ModelITransformer trainedModel = est.Fit(dataReader);
//Step 5. Make predictions using your modelvar predictionEngine = ctx.Model .CreatePredictionEngine<MyInput, MyOutput>(trainedModel);
var sampleStatement = new MyInput { Text = "This is a horrible movie" };
var prediction = predictionEngine.Predict(sampleStatement);
 
推荐关注Blazor~



欢迎在MIXLAB的前端社群里进行交流,

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多