Winform 使用示例


1、打开Visual Studio创建一个 Windows 窗体应用 项目

选择对应的Net版本,目前Hcf支持6.0、7.0、8.0的版本


2、打开Nuget包管理器,搜索HcfNS.Core

image.png

点击安装


3、在程序根目录创建一个Startup.cs的类文件,继承自IStartUp

    /// <summary>
    /// Startup
    /// </summary>
    public class Startup : IStartUp
    {
        /// <summary>
        /// ConfigureServices
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {

        }

        /// <summary>
        /// Configure
        /// </summary>
        /// <param name="provider"></param>
        public void Configure(IServiceProvider provider)
        {
        }
    }


4、修改Program.cs代码如下

using Hcf.Hosting;
using Hcf.Dependency;

namespace WinFormsApp1
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.

            AppHost.UseStartup(new Startup());

            ApplicationConfiguration.Initialize();
            Application.Run(ServiceLocator.Instance.GetService<Form1>());//Form1为入口程序,请根据实际情况修改
        }
    }
}


5、配置Startup启动类

    /// <summary>
    /// Startup
    /// </summary>
    public class Startup : IStartUp
    {
        /// <summary>
        /// 配置服务
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddPackManager<PackManager>()//添加包管理器
                    .AddPack<HcfCorePack>()//添加基础包
                    .AddPack<LoggingPack>()//添加日志包
                    .AddPack<FileLoggingPack>();//添加日志文件包

            services.AddScoped<Form1>();//Form1为入口程序,请根据实际情况修改
        }

        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="provider"></param>
        public void Configure(IServiceProvider provider)
        {
            //使用框架包
            provider.UsePack();
        }
    }


6、启动程序


当然我们也为您提供脚手架安装,具体请看 Winform脚手架Cli安装