国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:34:29
文檔

詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板

詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板:本文主要介紹了使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板,分享給大家,具體如下: 描述 當(dāng)我們安裝完 DotNetCore 3.0 版本的 SDK 后,我們就可以創(chuàng)建基于 DotNetCore 的 WPF 項(xiàng)目模板,通過如下 CLI 可以方便快捷的創(chuàng)建并運(yùn)行我們的項(xiàng)目: dotnet
推薦度:
導(dǎo)讀詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板:本文主要介紹了使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板,分享給大家,具體如下: 描述 當(dāng)我們安裝完 DotNetCore 3.0 版本的 SDK 后,我們就可以創(chuàng)建基于 DotNetCore 的 WPF 項(xiàng)目模板,通過如下 CLI 可以方便快捷的創(chuàng)建并運(yùn)行我們的項(xiàng)目: dotnet

本文主要介紹了使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板,分享給大家,具體如下:

描述

當(dāng)我們安裝完 DotNetCore 3.0 版本的 SDK 后,我們就可以創(chuàng)建基于 DotNetCore 的 WPF 項(xiàng)目模板,通過如下 CLI 可以方便快捷的創(chuàng)建并運(yùn)行我們的項(xiàng)目:

dotnet new wpf -n WpfApp
cd WpfApp
dotnet restore
dotnet run

做過 WPF 開發(fā)的朋友都知道,這個(gè)項(xiàng)目模板肯定不符合我們的預(yù)期,我們希望我們的項(xiàng)目模板能夠加入 MVVM 的默認(rèn)代碼段,并且能夠和 DotNetCore 緊密合作,這樣豈不是更加方便了嗎? 所以本文使用 MVVM 的一種實(shí)現(xiàn) MvvmLightStd10 來教大家如何創(chuàng)建一個(gè)我們理想的項(xiàng)目模板。

操作

首先,我們基于 DotNetCore 3.0 創(chuàng)建一個(gè)原始的 WPF 項(xiàng)目模板,然后引用如下庫(kù):

  1. Microsoft.Extensions.DependencyInjection
  2. MvvmLightLibsStd10

可通過執(zhí)行 cli 命令進(jìn)行安裝

dotnet add package Microsoft.Extensions.DependencyInjection
dotnet add package MvvmLightLibsStd10

注:因?yàn)槲覀兪褂昧?DotNetCore,所以我們盡量讓我們安裝的第三方包是基于 .NET Standard 方式來實(shí)現(xiàn)。

然后,嘗試修改我們的這個(gè)項(xiàng)目,把它改成我們以后期望創(chuàng)建的項(xiàng)目模板的樣子。可以參考我的如下修改:

項(xiàng)目結(jié)構(gòu)如下圖所示:

其中,src\Models\DataItem.cs 的示例代碼如下所示:

using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp.Models
{
 public class DataItem
 {
 public string Title { get; private set; }

 public DataItem(string title)
 {
 Title = title;
 }
 }
}

src\Models\IDataService.cs 的示例代碼如下所示:

using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp.Models
{
 public interface IDataService
 {
 void GetData(Action<DataItem, Exception> callback);
 }
}

src\Models\DataService.cs 的示例代碼如下所示:

using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp.Models
{
 public class DataService : IDataService
 {
 public void GetData(Action<DataItem, Exception> callback)
 {
 var item = new DataItem("Hello .NET Core!");
 callback(item, null);
 }
 }
}

src\ViewModels\MainViewModel.cs 的示例代碼如下所示:

using GalaSoft.MvvmLight;
using WpfApp.Models;

namespace WpfApp.ViewModels
{
 public class MainViewModel : ViewModelBase
 {
 private readonly IDataService _dataService;

 private string _welcomeTitle;
 public string WelcomeTitle
 {
 get { return _welcomeTitle; }
 set { Set(ref _welcomeTitle, value); }
 }

 public MainViewModel(IDataService dataService)
 {
 _dataService = dataService;
 _dataService.GetData(
 (item, error) =>
 {
 if (error != null)
 {
 return;
 }

 WelcomeTitle = item.Title;
 });
 }
 }
}

src\Views\MainView.xaml 的示例代碼如下所示:

<Window
 x:Class="WpfApp.Views.MainView"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:local="clr-namespace:WpfApp"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 Title="MainWindow"
 Width="800"
 Height="450"
 mc:Ignorable="d">
 <Grid>
 <Label
 HorizontalAlignment="Center"
 VerticalAlignment="Center"
 Content="{Binding WelcomeTitle}"
 FontSize="40" />
 </Grid>
</Window>

src\Views\MainView.xaml.cs 的示例代碼如下所示:

using System.Windows;
using WpfApp.ViewModels;

namespace WpfApp.Views
{
 public partial class MainView : Window
 {
 public MainView(MainViewModel vm)
 {
 InitializeComponent();
 this.DataContext = vm;
 }
 }
}

src\App.xaml 的示例代碼如下所示:

<Application
 x:Class="WpfApp.App"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="clr-namespace:WpfApp" />

src\App.xaml.cs 的示例代碼如下所示:

using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using WpfApp.Models;
using WpfApp.ViewModels;
using WpfApp.Views;

namespace WpfApp
{
 public partial class App : Application
 {
 public ServiceProvider ServiceProvider { get; private set; }

 protected override void OnStartup(StartupEventArgs e)
 {
 base.OnStartup(e);

 var serviceCollection = new ServiceCollection();
 ConfigureServices(serviceCollection);

 ServiceProvider = serviceCollection.BuildServiceProvider();

 var mainWindow = ServiceProvider.GetRequiredService<MainView>();
 mainWindow.Show();
 }

 private void ConfigureServices(ServiceCollection services)
 {
 services.AddTransient<MainView>();
 services.AddTransient<MainViewModel>();

 services.AddScoped<IDataService, DataService>();
 }
 }
}

修改完畢后嘗試編譯運(yùn)行我們的項(xiàng)目,確保可以正常編譯運(yùn)行。

之后,在我們的項(xiàng)目根目錄 src 下新建一個(gè) .template.config 文件夾,然后在里面新建一個(gè) template.json 文件,進(jìn)行如下示例配置:

{
 "$schema": "http://json.schemastore.org/template",
 "author": "hippiezhou <hippiezhou@outlook.com>",
 "classifications": ["wpf", "mvvmlight", "Dependency Injection"],
 "name": "wpf mvvmlight: use dotnetcore to create wpf with mvvmlight.",
 "tags": {
 "language": "C#",
 "type": "project"
 },
 "identity": "wpf.mvvmlight",
 "shortName": "wpf-mvvmlight",
 "sourceName": "wpf.mvvmlight",
 "preferNameDirectory": true
}

最后,打開我們的終端,將目錄切換至當(dāng)前項(xiàng)目目錄下(就是 .template.config 所在的目錄),然后執(zhí)行下述安裝操作

dotnet new -i C:\Users\hippieZhou\Desktop\helloworld\wpfapp

此時(shí),我們的項(xiàng)目模板會(huì)被打包到 DotNetCore 的 CLI 中,如下圖所示:

同時(shí),在 C:\Users\hippieZhou.templateengine\dotnetcli\v3.0.100-preview3-010431 目錄下的以 templatecache.json 結(jié)尾的 JSON 文件內(nèi)容也會(huì)發(fā)生修改,會(huì)在 TemplateInfo 結(jié)點(diǎn)下新增一個(gè)如下的節(jié)點(diǎn)內(nèi)容:

 {
 "ConfigMountPointId": "f3861181-7a43-4fc5-ab1c-12d95e734c0a",
 "Author": "hippiezhou <hippiezhou@outlook.com>",
 "Classifications": [
 "wpf",
 "mvvmlight",
 "Dependency Injection"
 ],
 "DefaultName": null,
 "Description": "",
 "Identity": "wpf.mvvmlight",
 "GeneratorId": "0c434df7-e2cb-4dee-b216-d7c58c8eb4b3",
 "GroupIdentity": "",
 "Precedence": 0,
 "Name": "wpf mvvmlight: use dotnetcore to create wpf with mvvmlight.",
 "ShortNameList": [
 "wpf-mvvmlight"
 ],
 "Tags": {
 "language": {
 "Description": null,
 "ChoicesAndDescriptions": {
 "C#": ""
 },
 "DefaultValue": "C#"
 },
 "type": {
 "Description": null,
 "ChoicesAndDescriptions": {
 "project": ""
 },
 "DefaultValue": "project"
 }
 },
 "CacheParameters": {
 "name": {
 "DataType": "string",
 "DefaultValue": null,
 "Description": "The default name symbol"
 }
 },
 "ConfigPlace": "/.template.config/template.json",
 "LocaleConfigMountPointId": "00000000-0000-0000-0000-000000000000",
 "LocaleConfigPlace": null,
 "HostConfigMountPointId": "00000000-0000-0000-0000-000000000000",
 "HostConfigPlace": null,
 "ThirdPartyNotices": null,
 "BaselineInfo": {},
 "HasScriptRunningPostActions": false,
 "ConfigTimestampUtc": null
},

注:如果后期我們不慎將我們的模板刪除了,我們通過刪除掉這兩個(gè)文件里面對(duì)應(yīng)的模板節(jié)點(diǎn)就可以在 CLI 中取消應(yīng)用了。

我們可以使用下述操作進(jìn)行測(cè)試一下:

# 使用我們自定義的項(xiàng)目模板,創(chuàng)建 wpf 項(xiàng)目
dotnet new wpf-mvvmlight -n test

cd test

dotnet restore

dotnet run

如果不出意外的話,我們就可以看到這個(gè)項(xiàng)目的代碼段和我們自定義的模板代碼段是一樣的。

如果卸載我們的項(xiàng)目模板可以使用如下命令:

dotnet new -u C:\Users\hippieZhou\Desktop\helloworld\wpfapp

注:我們需要確保我們的自定義模板不能丟失,要不然到時(shí)候就卸載就麻煩了(至少目前看來是這樣的)。

關(guān)于如何將我們的自定義模板可以上傳到 NuGet 供別人下載使用,這里就不做介紹了,具體操作可參考園里介紹如何在 DotNetCore MVC 中打造自己的項(xiàng)目模板方法是一樣的。我在本文中的創(chuàng)建的代碼模板也不會(huì)提交上去,還是等著 MVVMLight 的原作者 Laurent Bugnion 來操刀會(huì)好一些。

總結(jié)

本文介紹了如何通過 DotNet CLI 來創(chuàng)建自定義的 WPF 項(xiàng)目模板。在實(shí)際的使用過程中,CLI 的功能和支持的參數(shù)會(huì)更多,所以感興趣的朋友可以自行研究。

相關(guān)參考

how-to-create-a-dot-net-new-project-template-in-dot-net-core

打造自己的.NET Core項(xiàng)目模板

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板

詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板:本文主要介紹了使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板,分享給大家,具體如下: 描述 當(dāng)我們安裝完 DotNetCore 3.0 版本的 SDK 后,我們就可以創(chuàng)建基于 DotNetCore 的 WPF 項(xiàng)目模板,通過如下 CLI 可以方便快捷的創(chuàng)建并運(yùn)行我們的項(xiàng)目: dotnet
推薦度:
標(biāo)簽: cli dotNet 使用dotnet
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产精品久久久久久久久久久久 | 亚洲欧美另类自拍第一页 | 国产精品免费视频播放 | 中文国产成人精品少久久 | 亚洲欧美一区二区三区孕妇 | 亚洲视频第一页 | 日韩成人免费在线 | 欧美爱爱网址 | 日韩欧美色图 | 国产全黄a一级毛片 | 国产欧美日韩第一页 | 日韩午夜在线视频 | 91福利国产在线观一区二区 | 国产黄色片一级 | 亚洲国产成人影院播放 | 国产区免费在线观看 | 影音先锋亚洲综合小说在线 | 国产成人精品一区二三区在线观看 | 最刺激黄a大片免费观看 | 91久久精品国产免费一区 | 亚洲一区二区在线 | 中文字幕免费观看 | 国产精品视频一区二区三区 | 97久久综合区小说区图片专区 | 欧美日韩三区 | 亚洲啪啪网址 | 久久婷婷久久一区二区三区 | 国产精品高清在线观看 | 国产日韩亚洲 | 亚洲图片欧美在线 | 中文字幕免费观看 | 欧美激情综合亚洲一二区 | 中文欧美日韩 | 欧美日韩三级在线 | 欧美日韩亚洲视频 | 欧美 韩国 精品 另类 综合 | www日韩 | 久久久国产精品视频 | 欧美亚洲综合另类在线观看 | 欧美亚洲日本国产 | 狠狠干欧美 |