.Net6 or .Net Core界面程序依赖注入实现Caliburn.Micro

依赖注入的优点

(1)有效地设计服务及其依赖关系。

(2)防止多线程问题。

(3)防止内存泄漏。

(4)防止潜在的错误。

Caliburn.Micro主要用在MVVM, WPF, WinRT, UWP, Xamarin, Android, iOS, CoC, Convention, MVP, PM, Screen, Coroutine, Behavior, Model-View-ViewModel, Presentation, UI, ViewModel, Caliburn就是说控制台应用是用不了Caliburn.Micro的

代码下载

UnityDataServiceGui_20220316-C#文档类资源-CSDN下载UnityDataServiceGui_20220316更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/g313105910/84993382

一、创建项目

创建WPF项目,使用.net6

引入Caliburn.Micro

新建Bootstrapper类,内容如下

using Caliburn.Micro;
using FileLogger;
using System;
using System.Collections.Generic;
using UnityDataServiceGui.Modules.Main.ViewModels;

namespace UnityDataServiceGui
{
    public class Bootstrapper : BootstrapperBase
    {
        private SimpleContainer _container;

        public Bootstrapper()
        {
            Initialize();
        }

        protected override void Configure()
        {
            _container = new SimpleContainer();
            _container.Singleton<IWindowManager, WindowManager>();
            _container.Singleton<IEventAggregator, EventAggregator>();
            _container.Singleton<Log4netLogger>();
            //一定要注入进去,否则打不开窗体
            _container.Singleton<MainWindowViewModel>();

        }
        protected override object GetInstance(Type service, string key)
        {
            return _container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return _container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);
        }

        protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
        {
            try
            { 
                var logger = IoC.Get<Log4netLogger>();
                logger.Initialize("UnityDataServiceGui");
                logger.LogLevel = LoggingEventType.Debug;
                //打开窗口方法1
                var windowManager = IoC.Get<IWindowManager>();
                var mainWindows = IoC.Get<MainWindowViewModel>();
                windowManager.ShowWindowAsync(mainWindows);
                //打开窗口方法2
                //DisplayRootViewFor<MainWindowViewModel>();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

引入依赖

新建目录、窗体、mode,MainWindowViewModel和MainWindowView的名称是约定俗成

MainWindowViewModel.cs引入Caliburn.Micro

修改App.xmal,必须添加<local:Bootstrapper x:Key="Bootstrapper"/>,作为启动项

<Application x:Class="UnityDataServiceGui.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:UnityDataServiceGui">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="Bootstrapper"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

二、运行项目 

三、日志接口注入测试

项目改成控制台应用程序,这样可以看到Console输出

测试代码

namespace UnityDataServiceGui.Modules.Main.ViewModels
{
    public class MainWindowViewModel : Screen
    {
        readonly Log4netLogger _logger;
        public MainWindowViewModel(Log4netLogger logger)
        {
            _logger = logger;
            _logger.Warn(typeof(MainWindowViewModel),"这是一个测试程序!");
        }
    }
}

测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Caliburn.Micro 中,`ImportingConstructor` 是一个特性,用于标记类的构造函数,表示该构造函数需要进行依赖注入。当容器需要创建该类的实例时,会自动将标记了 `Import` 特性的参数注入到构造函数中。 例如,假设有以下类: ```csharp public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Do something"); } } public class ViewModel : Screen { private readonly IService _service; [ImportingConstructor] public ViewModel(IService service) { _service = service; } public void DoWork() { _service.DoSomething(); } } ``` 在上面的代码中,`ViewModel` 类有一个需要进行依赖注入的构造函数,并使用了 `ImportingConstructor` 特性进行标记。`IService` 是一个接口,`Service` 是实现该接口的类。 当需要创建 `ViewModel` 类的实例时,Simple Container 会自动扫描程序集中标记了 `Export` 特性的类和接口,并将它们注册到容器中。因此,当需要创建 `ViewModel` 实例时,Simple Container 会自动将 `Service` 类的实例注入到 `ViewModel` 的构造函数中。这样,在调用 `ViewModel` 实例的 `DoWork` 方法时,就可以通过 `_service` 成员变量访问 `Service` 类的实例了。 总之,Caliburn.Micro 中的 `ImportingConstructor` 特性可以帮助我们进行构造函数注入,使得容器能够自动将依赖注入到类的构造函数中,从而实现依赖注入

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花开花落的个人博客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值