WPF中如何用Autofac结合Microsoft MVVM Toolkit实现ViewModelLocator模式
实现Autofac与Microsoft MVVM Toolkit的ViewModel定位方案
1. 构建静态Autofac容器管理类
先创建一个全局静态类,负责初始化和维护Autofac容器,提供统一的服务解析入口:
using Autofac; public static class AutofacContainer { private static IContainer _container; // 初始化容器,接收注册配置逻辑 public static void Initialize(Action<ContainerBuilder> configure) { var builder = new ContainerBuilder(); configure(builder); _container = builder.Build(); } // 泛型解析方法 public static T Resolve<T>() { return _container.Resolve<T>(); } // 非泛型解析方法 public static object Resolve(Type type) { return _container.Resolve(type); } }
2. 实现替代ViewModelLocator的定位类
为了和MVVMLight的使用习惯对齐,创建一个ViewModelLocator类,通过静态容器解析ViewModel:
public class ViewModelLocator { // 每个属性对应一个ViewModel实例,由Autofac负责创建和注入依赖 public MainViewModel MainViewModel => AutofacContainer.Resolve<MainViewModel>(); public SettingsViewModel SettingsViewModel => AutofacContainer.Resolve<SettingsViewModel>(); // 按需添加其他ViewModel属性 }
3. 在App启动时初始化容器
在App.xaml.cs的OnStartup方法中完成Autofac的服务与ViewModel注册:
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); AutofacContainer.Initialize(builder => { // 注册业务服务(根据需求选择生命周期:SingleInstance/InstancePerDependency等) builder.RegisterType<DataService>().As<IDataService>().SingleInstance(); builder.RegisterType<NavigationService>().As<INavigationService>().InstancePerDependency(); // 注册ViewModel,生命周期按需配置 builder.RegisterType<MainViewModel>().InstancePerDependency(); builder.RegisterType<SettingsViewModel>().SingleInstance(); }); // 启动主窗口 var mainWindow = new MainWindow(); mainWindow.Show(); }
4. XAML中配置与使用
在App.xaml中注册ViewModelLocator为静态资源:
<Application x:Class="YourAppNamespace.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:YourAppNamespace" Startup="OnStartup"> <Application.Resources> <local:ViewModelLocator x:Key="Locator" /> </Application.Resources> </Application>
在视图中绑定DataContext,用法和MVVMLight完全一致:
<UserControl x:Class="YourAppNamespace.Views.SettingsView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <UserControl.DataContext> <Binding Path="SettingsViewModel" Source="{StaticResource Locator}" /> </UserControl.DataContext> <!-- 视图内容 --> </UserControl>
5. MVVM Toolkit的ViewModel示例
ViewModel正常继承ObservableObject,通过构造函数接收Autofac注入的服务:
using Microsoft.Toolkit.Mvvm.ComponentModel; public class MainViewModel : ObservableObject { private readonly IDataService _dataService; private string _welcomeText; public string WelcomeText { get => _welcomeText; set => SetProperty(ref _welcomeText, value); } public MainViewModel(IDataService dataService) { _dataService = dataService; WelcomeText = _dataService.GetWelcomeMessage(); } }
关键注意点
- Autofac的生命周期配置:根据业务场景选择
SingleInstance(全局单例)、InstancePerDependency(每次解析新实例)等模式 - 若不想额外维护
ViewModelLocator,也可以直接在XAML中用静态绑定调用AutofacContainer.Resolve,但ViewModelLocator能更好地兼容原有的代码结构 - 确保所有需要注入的服务和ViewModel都在容器初始化时完成注册
内容的提问来源于stack exchange,提问作者Werner
相关产品推荐
相关产品推荐

