带有参数化构造函数的 Wpf 用户控件 [英] Wpf usercontrol with parameterised constructor

查看:88
本文介绍了带有参数化构造函数的 Wpf 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Microsoft Unity 和依赖注入,因此我们为用户控件设置了参数化构造函数.如何使用 XAML 将此依赖项注入用户控件?

We are using Microsoft Unity and dependency injection and so we have parametrised constructor for the usercontrol. How to inject this dependency into usercontrol using XAML?

我在 XAML 中添加了用户控件,如下所示.

I have added the usercontrol in XAML as below.

xmlns:usrRefundArrivalProcessor="Ttl.Refunds.Wpf.Dashboad.Application.Usercontrols;assembly=Ttl.Refunds.Wpf.Dashboad.Application"

推荐答案

依赖注入并不意味着参数化构造函数.实际上,如果您查看 Unity 附带的示例,大部分依赖项注入都是由具有 [Dependency] 属性的属性完成的.

Dependency injection does not imply parameterized constructors. In fact, if you look at the samples that come with Unity, most of the dependency injection is done by properties with the [Dependency] attribute.

Unity 与 XAML 配合得很好,但前提是您不使用参数化构造函数.使用具有 [Dependency] 属性的属性将您的 UserControl 转换为获取其依赖项,并且您可以轻松地使用 XAML.

Unity works very well with XAML, but only if you don't use parameterized constructors. Convert your UserControl to take its dependencies using properties with the [Dependency] attribute, and you can easily use XAML.

public class MyUserControl : UserControl
{
  [Dependency]
  public ISomething Something { get; set; }

  [Dependency]
  public IWhatever Whatever { get { return (IWhatever)GetValue(WhateverProperty); } set { SetValue(WhateverProperty, value); }
  public readonly DependencyProperty WhateverProperty = DependencyProperty.Register("Whatever", typeof(IWhatever), typeof(MyUserControl));

  ...
}

请注意,[Dependency] 属性可以声明为 DependencyProperty,也可以声明为普通的 CLR 属性,如上所示.这听起来像是令人困惑的命名法,但实际上非常简单.

Note that a [Dependency] property can be declared either as a DependencyProperty or as a plain CLR property, as shown above. This sounds like confusing nomenclature but in practice it is very simple.

要在 XAML 中指定 UnityContainer 并获得自动配置,只需创建一个继承的附加属性UnityHelper.Container",其 PropertyChangedCallback 只需调用指定容器上的 BuildUp 并传入对象的类型和对象:

To specify the UnityContainer in XAML and get automatic configuration, just create an inherited attached property "UnityHelper.Container" whose PropertyChangedCallback simply calls BuildUp on the specified container and passes in the object's type and the object:

public class UnityHelper
{
  public static IUnityContainer GetContainer(DependencyObject obj) { return (IUnityContainer)obj.GetValue(ContainerProperty); }
  public static void SetContainer(DependencyObject obj, IUnityContainer value) { obj.SetValue(ContainerProperty, value); }
  public static readonly DependencyProperty ContainerProperty = DependencyProperty.RegisterAttached("Container", typeof(IUnityContainer), typeof(UnityHelper), new FrameworkPropertyMetadata
  {
    Inherits = true,
    PropertyChangedCallback = (obj, e) =>
    {
      var container = e.NewValue as IUnityContainer;
      if(container!=null)
      {
        var element = obj as FrameworkElement;
        container.BuildUp(obj.GetType(), obj, element==null ? null : element.Name);
      }
    }
  });
}

现在您可以为根窗口分配一个 UnityContainer,您的整个应用程序都将使用它,例如您可以在窗口的构造函数中执行以下操作:

Now you can assign a UnityContainer to your root window and your entire application will use it, for example you could do it in your window's constructor as follows:

UnityHelper.SetContainer(this, new UnityContainer() ...);

或者您可以在树的任何所需级别使用 XAML 分配统一容器:

Or you can assign the unity container using XAML at any desired level of the tree:

<UserControl ...
  my:UnityHelper.Container="{DynamicResource MainUnityContainer}" />

说了这么多,我想你会发现 WPF 的高级数据绑定功能和资源字典一起消除了人们可能想要使用 Unity 的 98% 的原因.从长远来看,您可能会发现远离 Unity 并使用简单的 MVVM 会更好.至少,在开发大量依赖 Unity 进行依赖注入的代码之前,我会在测试应用程序上尝试纯 MVVM,以了解它是如何工作的.

Having said all that, I think you'll will find that WPF's advanced data binding features and resource dictionaries together eliminate 98% of the reasons why a person might want to use Unity in the first place. You may find it better in the long run to move away from Unity and go with simple MVVM. At the very least I would try pure MVVM on a test application to see how it works before developing much code relying on Unity for dependency injection.

这篇关于带有参数化构造函数的 Wpf 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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