WPF ToolkitMVVM IOC IServiceConllection

用微软自带的 IOC 需要安装

csharp 复制代码
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 定义服务通道
        /// </summary>
        public IServiceProvider Services { get; }

        /// <summary>
        /// 设置当前正在使用的App实例
        /// 因为父类Application有这个属性,子类要隐藏父类,需要用new修饰
        /// </summary>
        public new static App Current=> (App)Application.Current;
        public App()
        {
            IServiceCollection sc= new ServiceCollection();
            sc.AddSingleton(typeof(ILog), typeof(Log));
            sc.AddTransient(typeof(MainViewModel));
            this.Services=sc.BuildServiceProvider();
        }
    }

}

案例

相关推荐
nap1171 天前
WPF 中常用的 LINQ 语句
wpf
做个文艺程序员4 天前
MQ第12篇:AI原生消息架构演进:从MQ到事件驱动的全链路设计
架构·消息队列·wpf·事件驱动架构·ai-native·cqrs
donk13146 天前
React Native for OpenHarmony 实战:三方库 react-native-circular-action-menu 的鸿蒙化适配指南
javascript·分布式·react native·react.js·wpf·harmonyos
donk13147 天前
React Native for OpenHarmony 实战:三方库 react-native-step-indicator 的鸿蒙化适配指南
分布式·react native·react.js·wpf·harmonyos
He BianGu7 天前
【笔记】WPF中RenderOptions类的详细功能
笔记·wpf
VidDown7 天前
上线三个月后的第一次事故:一个 Django 服务的日志、监控与故障复盘
数据库·python·django·wpf·音视频·视频编解码
He BianGu7 天前
【笔记】WPF中CachingHint的功能
笔记·wpf
He BianGu7 天前
【笔记】在WPF中BitmapScalingMode的作用
笔记·wpf
Vae_Mars8 天前
幂等性学习笔记
笔记·学习·wpf