C#開發可以用 Blazor 在今後寫前端渲染的網頁了!

- C #(C sharp)是什麼?

C#是微軟公司發佈的一種面向對象的、運行於.NET Framework和.NET C ore(完全開源,跨平臺)之上的高級程序設計語言。

- 爲什麼會出現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 Context

var ctx = new MLContext();


//Step 2. Read in the input data for model training

IDataView dataReader = ctx.Data

.LoadFromTextFile<MyInput>(dataPath, hasHeader: true);


//Step 3. Build your estimator

IEstimator<ITransformer> est = ctx.Transforms.Text

.FeaturizeText("Features", nameof(SentimentIssue.Text))

.Append(ctx.BinaryClassification.Trainers

.LbfgsLogisticRegression("Label", "Features"));


//Step 4. Train your Model

ITransformer trainedModel = est.Fit(dataReader);


//Step 5. Make predictions using your model

var predictionEngine = ctx.Model

.CreatePredictionEngine<MyInput, MyOutput>(trainedModel);


var sampleStatement = new MyInput { Text = "This is a horrible movie" };


var prediction = predictionEngine.Predict(sampleStatement);


推薦關注Blazor~

歡迎在 MIXLAB的前端社羣 裏進行交流,

讚賞後可得羣二維碼

相關文章