我如何注入依赖于WPF用户控件 [英] How do I inject dependencies to user controls in WPF

查看:244
本文介绍了我如何注入依赖于WPF用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是透明地注入依赖(使用IOC容器),用户控件在WPF中的最佳方法是什么?

What is the best way to transparently inject dependencies (using IOC container) to user controls in WPF?

我认为用户控件XAML的窗口或其他用户控件的一部分。 此外,我觉得父母(谁是)不应该为此事负责。解决方案从父手动注入依赖看起来不够干净我。我想避免明确管理我的组件的依赖关系,因为它voilates国际奥委会的想法。

I assume that user controls are part of XAML for the window or other user controls. Also I think parent (whoever it is) should not be responsible for this. Solution for manually injecting dependencies from parent looks not clean enough for me. I want to avoid managing the dependencies of my components explicitly as it voilates the idea of IOC.

时,当正在创建的逻辑树被提出的任何事件,所以我可以拦截并注入我的依赖呢?

Is any event which is raised when logical tree is being created so I can intercept it and inject my dependencies?

编辑: 通过我的依赖性也意味着视图模型,控制器,presenter(任何模式时)

by dependencies I also mean ViewModel, Controller, Presenter (whatever pattern is used)

谢谢, 安德烈

推荐答案

一个解决问题的可能途径是去与视图模型第一的方针,并使用约定优于配置。

One of the possible ways to solve the problem is to go with "ViewModel First" approach and using convention over configuration.

自创MVVM框架通过罗布·艾森伯格提供更多这方面的信息。

Build Your Own MVVM Framework by Rob Eisenberg provides more details on this

这篇关于我如何注入依赖于WPF用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆