首页 > 其他分享 >原生WPF使用IOC容器

原生WPF使用IOC容器

时间:2024-05-29 23:25:18浏览次数:27  
标签:原生 ServiceCollection ServiceProvider App void services WPF IOC public

1、删除App.xaml中的 StartupUri=""
2、改造App.xaml.cs

 public partial class App : Application
 {
     public App()
     {
         ServiceProvider = GetServiceProvider();
     }

     private IServiceProvider GetServiceProvider()
     {
         ServiceCollection services = new ServiceCollection();
         services.AddTransient<MainView>();
         services.AddTransient<MainViewModel>();
         return services.BuildServiceProvider();
     }

     public IServiceProvider ServiceProvider { get; set; }

     protected override void OnStartup(StartupEventArgs e)
     {       
         base.OnStartup(e);
         Initialized();
        
     }
     public void Initialized()
     {
         MainView mainView = ServiceProvider.GetService<MainView>();
         mainView.Show();
     }  
 }

标签:原生,ServiceCollection,ServiceProvider,App,void,services,WPF,IOC,public
From: https://www.cnblogs.com/guchen33/p/18221329

相关文章

  • WPF使用事件聚合器,实现任意页面跨页通信
    前言:最近几天有好几个小伙伴玩WPF,遇到不同页面,不知道要怎么传递消息。于是,我今天就来演示一个事件聚合器的玩法,采用prism框架来实现。作为福利,内容附带了主页面打开对话框时候直接通过参数传递消息的一个小例子,具体请自行围观。以下内容,创建wpf项目以及引用prism和实现依赖注入......
  • WPF Image enlarge via MouseWheel, selected image center does not shift
    //xaml<Windowx:Class="WpfApp123.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • ThinkEditor电子病历编辑器:一款优秀的原生电子病历编辑器
    设计理念信译(ThinkEditor)跨平台电子病历编辑器在技术端核心设计理念是跨平台和全结构化。在产品端的核心开发思想是实现医疗电子病历编辑器的全功能和低耦合。以产品创新为导向,以服务订制为宗旨提高合作厂商产品迭代速度,提升医疗产品竞争力,实现合作与共赢创新技术手段实......
  • wpf 捕获全局异常
    App(){this.Startup+=App_Startup;}privatevoidApp_Startup(objectsender,StartupEventArgse){this.DispatcherUnhandledException+=App_DispatcherUnhandledException;AppDomain.CurrentDomain.UnhandledException+=CurrentD......
  • .net 8 WPF发布程序只生成exe
    右击项目选择发布配置发布信息修改FolderProfile.pubxml文件内容ReleaseAnyCPUbin\Release\net8.0-windows\publish\win-x64</PublishDir>FileSystem<_TargetId>Folder</_TargetId>net8.0-windowswin-x64truefalsetruetrue<_SuppressWpfTrimError>......
  • 原生ceph更换故障硬盘过程
    准备1、确定故障硬盘所在节点的信息,避免操作错误 2确定故障osd的lvm信息 ceph-c8fd9380-1a1a-45e4-a43a-b36da6fdc560ceph-22b06ce3-0186-40cf-9095-e777bc458bf63确认数据盘对应的缓存盘(查看ceph部署时的日志)osd.9->sdb2 osd.81->sdc6更换步骤1禁止平台数......
  • WPF Image ZoomIn ZoomOut pan
    <Windowx:Class="WpfApp120.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Path Geometry PathFigureCollection PathFigure PathFigure.Segments PolyQuadra
    <Windowx:Class="WpfApp118.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF DrawingContext DrawingVisual OnRender()
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;......
  • WPF RingShape
    //csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.I......