• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Winform窗体使用IOC容器

武飞扬头像
游子吟i
帮助1

Winform窗体如何使用IOC容器呢?在nuget中添加Microsoft.Extensions.DependencyInjection

接着在Program类Main方法中添加一下代码

  1.  
    // 创建服务集
  2.  
    Build();
  3.  
    using (FmLogin fl = ServiceProvider.GetRequiredService<FmLogin>())
  4.  
    {
  5.  
    fl.ShowDialog();
  6.  
    if (fl.DialogResult == DialogResult.OK)
  7.  
    Application.Run(ServiceProvider.GetRequiredService<FmMain>());
  8.  
    }

在此类中继续补充以下代码

  1.  
    /// <summary>
  2.  
    /// 服务容器
  3.  
    /// </summary>
  4.  
    static IServiceCollection Services { get; set; }
  5.  
    /// <summary>
  6.  
    /// 服务管理者
  7.  
    /// </summary>
  8.  
    static IServiceProvider ServiceProvider { get; set; }
  9.  
    static void Build()
  10.  
    {
  11.  
    // 创建服务容器
  12.  
    Services = new ServiceCollection();
  13.  
    // 添加服务注册
  14.  
    ConfigureServices(Services);
  15.  
    // 创建服务管理者
  16.  
    ServiceProvider = Services.BuildServiceProvider();
  17.  
    }
  18.  
    /// <summary>
  19.  
    /// 注入服务
  20.  
    /// </summary>
  21.  
    /// <param name="services"></param>
  22.  
    static void ConfigureServices(IServiceCollection services)
  23.  
    {
  24.  
    // 注入日志
  25.  
    services.AddSingleton(typeof(ILogFactory<>),typeof(LogFactory<>));
  26.  
    // 注入窗体
  27.  
    RegisterForm();
  28.  
    // 注入IniHelper
  29.  
    services.AddScoped<IIniHelper, IniHelper>();
  30.  
     
  31.  
     
  32.  
    }
  33.  
    #region 注入窗体-开始
  34.  
    static void RegisterForm()
  35.  
    {
  36.  
    Type[]? types = Assembly.GetExecutingAssembly()?.GetExportedTypes();
  37.  
    if (types != null)
  38.  
    {
  39.  
    var descType = typeof(FormMarkAttribute);
  40.  
    var form = typeof(Form);
  41.  
    foreach (Type type in types)
  42.  
    {
  43.  
    // 类型是否为窗体,否则跳过,进入下一个循环
  44.  
    //if (type.GetTypeInfo != form)
  45.  
    // continue;
  46.  
     
  47.  
    // 是否为自定义特性,否则跳过,进入下一个循环
  48.  
    if (!type.IsDefined(descType, false))
  49.  
    continue;
  50.  
    // 强制为自定义特性
  51.  
    FormMarkAttribute? attribute = type.GetCustomAttribute(descType, false) as FormMarkAttribute;
  52.  
    // 如果强制失败或者不需要注入的窗体跳过,进入下一个循环
  53.  
    if (attribute == null || !attribute.IsIOC)
  54.  
    continue;
  55.  
    // 域注入
  56.  
    Services.AddScoped(type);
  57.  
    Console.WriteLine($"注入:{attribute.FormType.Namespace}.{attribute.FormType.Name},{attribute.Describe}");
  58.  
    }
  59.  
    }
  60.  
    }
  61.  
    #endregion 注入窗体-结束
学新通

以后你的注入只需要在 static void ConfigureServices(IServiceCollection services)这个方法中注入就行了

全景图:

学新通

 学新通

我把窗体也注入了,不过不是全部注入,是标记的并且是需要注入的,我们看FormMarkAttribute源码:

  1.  
    /// <summary>
  2.  
    /// Form窗体标记
  3.  
    /// </summary>
  4.  
    [AttributeUsage(AttributeTargets.Class)]
  5.  
    public class FormMarkAttribute : Attribute
  6.  
    {
  7.  
    /// <summary>
  8.  
    /// 描述内容
  9.  
    /// </summary>
  10.  
    public string Describe { get; private set; }
  11.  
    /// <summary>
  12.  
    /// 是否运行注入,默认false
  13.  
    /// </summary>
  14.  
    public bool IsIOC { get; private set; }
  15.  
    /// <summary>
  16.  
    /// 窗体类型
  17.  
    /// </summary>
  18.  
    public Type FormType { get; private set; }
  19.  
     
  20.  
    /// <summary>
  21.  
    /// 有参构造
  22.  
    /// </summary>
  23.  
    /// <param name="type">窗体反射类型</param>
  24.  
    public FormMarkAttribute(Type type)
  25.  
    {
  26.  
    this.FormType = type;
  27.  
    this.IsIOC = false;
  28.  
    this.Describe =String.Empty;
  29.  
    }
  30.  
    /// <summary>
  31.  
    /// 有参构造
  32.  
    /// </summary>
  33.  
    /// <param name="type">窗体反射类型</param>
  34.  
    /// <param name="describe">窗体描述</param>
  35.  
    public FormMarkAttribute(Type type, string describe)
  36.  
    {
  37.  
    this.Describe = describe;
  38.  
    this.IsIOC = false;
  39.  
    this.FormType = type;
  40.  
    }
  41.  
    /// <summary>
  42.  
    /// 有参构造
  43.  
    /// </summary>
  44.  
    /// <param name="type">窗体反射类型</param>
  45.  
    /// <param name="describe">是否需要注入</param>
  46.  
    public FormMarkAttribute(Type type, bool isIOC)
  47.  
    {
  48.  
    this.Describe = String.Empty;
  49.  
    this.IsIOC = isIOC;
  50.  
    this.FormType = type;
  51.  
    }
  52.  
    /// <summary>
  53.  
    /// 有参构造
  54.  
    /// </summary>
  55.  
    /// <param name="type">窗体反射类型</param>
  56.  
    /// <param name="describe">窗体描述</param>
  57.  
    /// <param name="isIOC">是否需要注入</param>
  58.  
    public FormMarkAttribute(Type type,string describe,bool isIOC)
  59.  
    {
  60.  
    this.Describe = describe;
  61.  
    this.IsIOC = isIOC;
  62.  
    this.FormType = type;
  63.  
    }
  64.  
    }
学新通

使用:

学新通

学新通

注意我的窗体继承的是FmCommonForm而不是Form,因为我可以在FmCommonForm里面写很多公共需要的方法、属性等;比如:全部窗体的icon图标,加载等待窗体等待。如下

学新通

需要源码的滴滴。。。 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkbjj
系列文章
更多 icon
同类精品
更多 icon
继续加载