soft.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>();
}
}
}
修改完毕后尝试编译运行我们的项目,确保可以正常编译运行。
之后,在我们的项目根目录 src 下新建一个 .template.config 文件夹,然后在里面新建一个 template.json 文件,进行如下示例配置:
{
"$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
}
最后,打开我们的终端,将目录切换至当前项目目录下(就是 .template.config 所在的目录),然后执行下述安装操作
dotnet new -i C:\Users\hippieZhou\Desktop\helloworld\wpfapp
此时,我们的项目模板会被打包到 DotNetCore 的 CLI 中,如下图所示:
同时,在 C:\Users\hippieZhou.templateengine\dotnetcli\v3.0.100-preview3-010431 目录下的以 templatecache.json 结尾的 JSON 文件内容也会发生修改,会在 TemplateInfo 结点下新增一个如下的节点内容:
{
"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":