WPF使用CommunityToolkit.Mvvm开发项目-App.xaml.cs配置

引入:using Microsoft.Extensions.DependencyInjection;DI容器

 

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace MedicalRecordSelfTerminal.WpfApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            //组件注入
            Services = ConfigureServices();
            InitializeComponent();
        }
        /// <summary>
        /// 设置当前正在使用的App实例
        /// </summary>
        public new static App Current => (App)Application.Current;
        /// <summary>
        /// 定义服务通道
        /// </summary>
        public IServiceProvider Services { get; }
        /// <summary>
        /// 需要注入的组件
        /// </summary>
        /// <returns></returns>
        private static IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();
            //services.AddTransient<DataGridViewModel>();
            //services.AddTransient<TabItemVM>();
            //services.AddSingleton<IUsersData, UsersData>();
            return services.BuildServiceProvider();
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Caliburn.Micro是一个轻量级的MVVM框架,它可以帮助我们更好地实现WPF应用程序的开发。下面是一些关于Caliburn.Micro框架的介绍和示例: 1.使用Caliburn.Micro实现MEF插件化开发,根据dll插件目录动态加载程序集和主程序的Tab页。 ```csharp // 在App.xaml.cs中添加以下代码 protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); // 创建插件管理器 var pluginManager = new PluginManager(); // 加载插件 pluginManager.LoadPlugins(); // 创建主窗口 var mainWindowViewModel = new MainWindowViewModel(pluginManager); // 显示主窗口 var mainWindow = new MainWindow(); mainWindow.DataContext = mainWindowViewModel; mainWindow.Show(); } ``` 2.CM框架搭建、事件发布与订阅、log打印、ioc容器、Conductor的用法,框架下的命名约定以及事件绑定、Emgucv进行模板匹配、右键菜单、单例模式等等。 ```csharp // ViewModelBase.cs public abstract class ViewModelBase : Screen { protected readonly IEventAggregator _eventAggregator; protected readonly ILog _logger; public ViewModelBase(IEventAggregator eventAggregator, ILog logger) { _eventAggregator = eventAggregator; _logger = logger; } // 发布事件 protected void Publish<TEvent>(TEvent @event) { _eventAggregator.PublishOnUIThread(@event); } // 订阅事件 protected void Subscribe<TEvent>(Action<TEvent> action) { _eventAggregator.Subscribe(action, ThreadOption.UIThread); } // 打印日志 protected void Log(string message) { _logger.Info(message); } } // ShellViewModel.cs public class ShellViewModel : Conductor<object>.Collection.OneActive { private readonly IWindowManager _windowManager; private readonly IEventAggregator _eventAggregator; private readonly ILog _logger; private readonly IContainer _container; public ShellViewModel(IWindowManager windowManager, IEventAggregator eventAggregator, ILog logger, IContainer container) { _windowManager = windowManager; _eventAggregator = eventAggregator; _logger = logger; _container = container; // 订阅事件 _eventAggregator.Subscribe(this); } // 打开新窗口 public void OpenWindow() { var viewModel = _container.Resolve<NewWindowViewModel>(); _windowManager.ShowDialog(viewModel); } // 关闭窗口 public void CloseWindow() { TryClose(); } // 处理事件 public void Handle(MyEvent @event) { // 处理事件 } } // ShellView.xaml <UserControl x:Class="MyApp.Views.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Menu Grid.Row="0"> <MenuItem Header="File"> <MenuItem Header="Open" cal:Message.Attach="OpenWindow" /> <MenuItem Header="Close" cal:Message.Attach="CloseWindow" /> </MenuItem> </Menu> <ContentControl Grid.Row="1" cal:View.Model="{Binding ActiveItem}" /> </Grid> </UserControl> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值