WPF中使用Unity [英] Using Unity in WPF

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

问题描述

我有统一2.0 App.xaml.cs内工作顺利进行登记,并且该类内解决。

I have Unity 2.0 working well within the App.xaml.cs to register and resolve within that class.

我的问题是关于最佳实践。

The question I have is regarding a best practice.

我有一些用户控件和其他类的,这也需要解决一些相同的和新接口和LT的; - >实现。问题是,世界上没有其他的方式来访问我在App.xaml.cs.创建的统一容器

I have a number of User Controls and other classes that also need to resolve some of the same and new Interface <-> implementations. The problem is theres no way to access the Unity container I created in the App.xaml.cs.

我不能使用构造函数或财产注入传递容器参考。

I cannot use constructor or property injection to pass on the container reference.


  1. 实在太多了(它是一个大的项目)

  2. 用户控制通过XAML添加

  3. 有项目
    相关的模块几个非常松散
    可以共享同一个容器
    的配置。

我宁愿不重新创建在需要访问该容器中的每个对象的配置文件的容器。

I would rather not re-create the container from a config file in each object that needs access to the container.

当需要在同一组件的不同的模块的服务相同的容器内的任何最佳实践建议?

Any best practice suggestions when the same container is needed as a service in various "modules" of the same assembly?

感谢。

推荐答案

我相信汇集控制的IoC 是痛......在代码中最少。也许有人会说,但IMO最佳做法,以避免这种痛苦是 MVVM 。您将有您可以自由使用构造的ViewModels 统一,并注入你需要进入他们的一切。你将不得不与绑定的ViewModels的意见,没有理由要知道任何事情比比皆是控制反转

I believe bringing together Controls and IoC is pain in the ... in the code at least. Probably somebody will argue but IMO the best practice to avoid this pain is MVVM. You will have viewModels which you can freely construct using Unity and inject everything you need into them. You will have views with bindings to viewModels with no reason to know anything abound inversion of control.

更新:基于评论:

App.xaml.cs:

App.xaml.cs:

    private void HandleStartup(object sender, StartupEventArgs e)
    {
        var container = CreateContainer(); // create IoC container
       var mainViewModel = container.Resolve<MainViewModel>();
        var shell = new Shell { DataContext = mainViewModel }; // main View
        MainWindow = shell;
        shell.Show();
    }



壳牌XAML例如:

Shell XAML example:

<UserControl>
     <StackPanel>
          <ContentPresenter Content="{Binding ViewModel1}" />
          <ContentPresenter Content="{Binding ViewModel2}" />
          <ContentPresenter Content="{Binding ViewModel3}" />
     </StackPanel>
</UserControl>



MainViewModel:

MainViewModel:

public class MainViewModel
{
     public ViewModel1 ViewModel1 { get; private set; }
     public ViewModel2 ViewModel2 { get; private set; }
     public ViewModel3 ViewModel3 { get; private set; }

     // this will be handled by IoC container
     public MainViewModel(ViewModel1 viewModel1, ViewModel2 viewModel2, ViewModel3 viewModel3)
    {
        ViewModel1 = viewModel1;
        ViewModel2 = viewModel2;
        ViewModel3 = viewModel3;
    }

在这样的意见将不知道国际奥委会并要在的ViewModels一切将成功注入。

In this way your views will be unaware of IoC and everything you want in viewModels will be successfully injected.

UPDATE2 DataTemplating带来查看的ViewModels 在一起:

UPDATE2 DataTemplating which brings Views and ViewModels together:

的App.xaml

<Application.Resources>
    <DataTemplate DataType="{x:Type local:ViewModel1}">
        <View1 />
    </DataTemplate>
</Application.Resources>

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

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