IoC 和用户界面

发布于 2024-07-09 07:41:11 字数 603 浏览 9 评论 0原文

我正在尝试在我的应用程序中使用 IoC 进行依赖项注入的最佳方式,但是我有一个小问题。

我正在 WPF 应用程序中使用 MVP 模式的松散实现。 本质上,演示者类被实例化,视图和任务(例如 EmployeePresenter 的 IEmployeeView 和 IEmployeeTask)被注入到演示者中。

我想使用 IoC 容器(我正在尝试 Unity,尽管我猜想这也会发生在其他容器上,例如 ninject 或 Structure Map),而不是手动注入这些实例,但是如果演示者是创建的(或从IoC 容器)在异步委托调用或事件线程(例如非 STA 线程)上,然后创建 WPF 窗口的新实例会引发以下异常:

当前构建操作(构建密钥 构建密钥[命名空间.Window1,null]) 失败:调用线程必须是 STA,因为UI组件很多 需要这个。

现在,我知道新的窗口实例等需要是 STA,但是即使必须在 STA 线程上创建 UI,是否也可以使用 IoC 容器进行依赖项注入?

从这个问题来看,正在解析的类/类型似乎是在解析时实例化的,而不是在注册时实例化的......

I'm trying to wrap my head around the best way to use IoC within my application for dependency injection, however I have a little issue.

I am using a loose implementation of the MVP pattern with a WPF app. Essentially, a presenter class is instantiated, and a view and task (e.g. IEmployeeView and IEmployeeTask for EmployeePresenter) are injected into the presenter.

I would like to use an IoC container (I'm trying out Unity, though I guess this would also happen with others such as ninject or Structure Map) instead of manually injecting these instances, however if the presenter is created (or resolved from an IoC container) on an async delegate call, or an event thread (e.g. not STA threaded) then creating a new instance of a WPF window throws the following exception:

The current build operation (build key
Build Key[namespace.Window1, null])
failed: The calling thread must be
STA, because many UI components
require this.

Now, I know that new window instances etc need to be STA, however is it possible to use an IoC Container to do the dependency injection even when the UI must be created on an STA thread?

From looking at this problem it would seem that the class/type being resolved is instantiated at the resolve time, not when its registered...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2024-07-16 07:41:11

我会说使用工厂来创建 Presenter 对象; 这样,您就可以在 STA 线程中的 PresenterFactory 中创建 Presenters 的通用实例,然后在需要时简单地将它们传递出去。

I would say to use a Factory to create the Presenter objects; that way, you can create your generic instances of Presenters from within your PresenterFactory in an STA thread, and then simply pass them out when required.

若相惜即相离 2024-07-16 07:41:11

您的问题与 IoC 无关,WPF 对象只能从创建它的同一线程访问 - 因此您必须在与 GUI 其余部分相同的线程上创建演示者(而不仅仅是任何 STA 线程)。

使用 Dispatcher.BeginInvoke 在主线程中运行代码并从那里调用 Ioc contianer。

Your problem have nothing to do with IoC, a WPF object can only be accessed from the same thread that created it - so you have to create your presenter on the same thread as the rest of your GUI (and not just any STA thread).

Use Dispatcher.BeginInvoke to run code in the main thread and call Ioc contianer from there.

涫野音 2024-07-16 07:41:11

您提到Unity,您是否考虑过使用也使用它的复合应用程序库? StockTrader 示例应用程序使用 Unity 将视图注入演示模型。如果您不想实际使用 CAL - 更多信息:
(http://msdn.microsoft.com/en-us/library/cc707890 .aspxhttp://www.codeplex.com/CompositeWPF

您可能仍然能够弄清楚他们是如何解决这个问题的。

You mention Unity, have you looked at using the Composite Application Library which also uses it? The StockTrader sample application uses Unity to inject views to presentation models.. If you don't want to actually use the CAL - more info:
(http://msdn.microsoft.com/en-us/library/cc707890.aspx or http://www.codeplex.com/CompositeWPF)

you might still be able to work out how they got around the issue..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文