设为首页 加入收藏

TOP

使用 DotNet CLI 创建自定义的 DotNetCore 3.0 WPF 项目模板(二)
2019-09-17 19:07:01 】 浏览:75
Tags:使用 DotNet CLI 创建 定义 DotNetCore 3.0 WPF 项目 模板
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":
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇20.1翻译系列:EF 6中自动数据迁.. 下一篇21.翻译系列:Entity Framework 6..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目