使用Autofac解决XAML中的依赖关系 [英] Resolving dependencies in XAML using Autofac

查看:105
本文介绍了使用Autofac解决XAML中的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个类MyResource,如下所示:

I have a class MyResource in my application that looks like this:

public class MyResource : IMyResource
{
    // ... whatever ...
}

当我在App.xaml.cs中初始化我的应用程序时,我使用Autofac有类似的东西:

And when I initialize my application in App.xaml.cs I have something like that using Autofac:

builder.Register<IMyResource>(container => new MyResource());

现在,我需要在WPF应用程序的Window中添加一个StaticResource,如下所示:

Now I need to add a StaticResource in a Window of my WPF application, something like this:

<Window.Resources>
    <local:MyResource x:Key="MyResource" />
</Window.Resources>

但是,当然,整个想法不是在这里引用MyResource的具体实例.此外,我可能需要在整个应用程序的不同WindowUserControl中使用MyResource的实例.因此,我想将MyResource的实例用作我的WindowStaticResource,该实例通过Autofac容器解析.我该如何实现?

But of course, the whole idea is not to reference a concrete instance of MyResource here. Moreover, I may need to use an instance of MyResource in different Windows or UserControls across my application. So I would like to use an instance of MyResource as a StaticResource for my Window that is resolved through the Autofac container. How can I achieve this?

我当时正在考虑将资源添加到我的Window的代码背后,但是这可能会导致我不需要的容器的依赖关系.

I was thinking of adding the resource in the code-behind of my Window, but it may create a dependency on my container which I don't want.

在初始化应用程序时,我还考虑在App.xaml.cs中做类似的事情:

I was also thinking of doing something like that in App.xaml.cs, when I initialize the application:

App.Current.MainWindow.Resources.Add("MyResource", container.Resolve<IMyResource>());

但是当我在XAML中使用资源时

But when I use the resource in my XAML

<ListBox ItemsSource="{Binding Source={StaticResource ResourceKey=MyResource}}"/>

我得到一个XAMLParseException,该异常内部消息指示找不到名为MyResource的资源.即使它起作用了,我也感觉有点臭.

I get an XAMLParseException which inner exception's message stating that the resource named MyResource cannot be found. And even if it was working, I feel like it's a bit smelly.

那么如何实现呢?有可能吗?如果不是,最好的方法是什么?

So how can this be achieved? Is it only possible? If not what are the best way to implement this?

推荐答案

执行以下步骤

  • 使用Autofac注册MyWindowMyResource.
  • IMyResource放在MyWindow的构造函数中(是的,您正在修改后面的代码,但您不是在引用容器.如果在后面的代码中没有代码,则可能是UserControl -然后确保有人将DataContext设置在某处)
  • DataContext设置为IMyResource的具体实例(在构造函数中),或者,如果您使用的是MVVM,则将该实例放入您的视图模型(也将在Autofac中注册).
  • 解决MyWindow
  • Register MyWindow and MyResource with Autofac.
  • Place IMyResource in the constructor of MyWindow (Yes, you are modifying the code behind, but you are not referring to your container. If you cannot have code in the Code-behind -- perhaps you are a UserControl --then make sure someone is setting the DataContext somewhere)
  • Set DataContext to your concrete instance of IMyResource (in the constructor), or if you are using MVVM, place the instance into your viewmodel (which would also be registered with Autofac).
  • Resolve MyWindow

在代码中:

MyWindow(IMyResource myResource) : this()
{
  DataContext = myResource;
}

如果您使用的是ViewModel(也已在Autofac中注册):

If you are using a ViewModel (also registered with Autofac):

MyWindow(MyViewModel viewModel) : this()
{
  DataContext = viewModel;
}

将此行添加到您的XAML:

Add this line to your XAML:

<Window.DataContext><local:IMyResource></Window.DataContext>

或者这个:

<Window.DataContext><local:MyViewModel></Window.DataContext>

然后您对ListBox的标记变得微不足道:

And then your markup for ListBox becomes trivial:

<ListBox ItemsSource="{Binding}"/>

或者,例如,使用viewmodel作为属性Items一样好:

Or, with the viewmodel, as the property Items, for instance, it's just as nice:

<ListBox ItemsSource="{Binding Items}"/>

这篇关于使用Autofac解决XAML中的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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