1、打开Visual Studio创建一个 WPF 应用程序 项目
选择对应的Net版本,目前Hcf支持6.0、7.0、8.0的版本
2、打开Nuget包管理器,搜索HcfNS.Core
点击安装
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、移除App.xaml中配置的StartupUri="MainWindow.xaml",改为通过动态创建方式
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<Application.Resources>
</Application.Resources>
</Application>
5、修改App.cs代码如下
using System.Windows;
using Hcf.Hosting;
using Hcf.Dependency;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// 启动
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
AppHost.UseStartup(new Startup());
var mainWindow = ServiceLocator.Instance.GetService<MainWindow>();//MainWindow为入口程序,根据实际项目修改
mainWindow.Show();
}
}
}
6、配置Startup启动类
/// <summary>
/// 启动类
/// </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<MainWindow>();//添加入口程序,根据实际修改
}
/// <summary>
/// 配置
/// </summary>
/// <param name="provider"></param>
public void Configure(IServiceProvider provider)
{
//使用框架包
provider.UsePack();
}
}
7、启动程序
当然我们也为您提供脚手架安装,具体请看 Wpf脚手架Cli安装