Unity:依赖注入

发布于 2024-09-05 07:08:20 字数 1951 浏览 2 评论 0原文

公共部分类 HTCmds :ResourceDictionary { 私有ICanvasService mCanvasService;

        [Dependency]
        public ICanvasService CanvasService
        {
            get { return mCanvasService; }
            set { mCanvasService = value; }
        }

        public HTCmds()
        {
            CopyCommand = new DelegateCommand<object>(this.Copy, this.CanCopy);
            ExitCommand = new DelegateCommand<object>(this.Exit);
        }

        public DelegateCommand<object> CopyCommand { get; private set; }
        public DelegateCommand<object> ExitCommand { get; private set; }
}

资源字典 Xaml:

<ResourceDictionary x:Class="HTCmds" 
                    x:ClassModifier="public"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:commands="clr-namespace:Commands;assembly=UIInfrastructure"
                    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
                    xmlns:local="clr-namespace:Commands.Commands">
    <local:HTCmds x:Key="thisobj"/>
    <commands:CommandReference x:Key="CopyCommandReference" Command="{Binding  Source={StaticResource thisobj}, Path=CopyCommand}"/>
    <commands:CommandReference x:Key="ExitCommandReference" Command="{Binding  Source={StaticResource thisobj}, Path=ExitCommand}"/>
</ResourceDictionary>

我已经注册了 ICanvasService,但它没有被注入到此类中。资源字典合并在 Windows 类的 xaml 文件中:

<ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Commands/HTCmds.xaml" />
     </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

ResourceDictionary 类有什么特定的内容吗?

谢谢&问候, 维沙尔。

public partial class HTCmds : ResourceDictionary
{
private ICanvasService mCanvasService;

        [Dependency]
        public ICanvasService CanvasService
        {
            get { return mCanvasService; }
            set { mCanvasService = value; }
        }

        public HTCmds()
        {
            CopyCommand = new DelegateCommand<object>(this.Copy, this.CanCopy);
            ExitCommand = new DelegateCommand<object>(this.Exit);
        }

        public DelegateCommand<object> CopyCommand { get; private set; }
        public DelegateCommand<object> ExitCommand { get; private set; }
}

Resource Dictionary Xaml:

<ResourceDictionary x:Class="HTCmds" 
                    x:ClassModifier="public"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:commands="clr-namespace:Commands;assembly=UIInfrastructure"
                    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
                    xmlns:local="clr-namespace:Commands.Commands">
    <local:HTCmds x:Key="thisobj"/>
    <commands:CommandReference x:Key="CopyCommandReference" Command="{Binding  Source={StaticResource thisobj}, Path=CopyCommand}"/>
    <commands:CommandReference x:Key="ExitCommandReference" Command="{Binding  Source={StaticResource thisobj}, Path=ExitCommand}"/>
</ResourceDictionary>

I've registered the ICanvasService but it's not getting injected in this class. Resource Dictionary is merged in the xaml file of a windows class:

<ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Commands/HTCmds.xaml" />
     </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

Is there something specific with ResourceDictionary class?

Thanks & Regards,
Vishal.

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

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

发布评论

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

评论(1

榕城若虚 2024-09-12 07:08:20

您的 HTCmds 对象是由 WPF 通过以下 XAML 行创建的:

<local:HTCmds x:Key="thisobj"/>

WPF 不了解 Unity,因此它不知道如何使用 Unity 解决依赖关系。您需要使用 UnityContainer.Resolve 解析对象。您不能依赖 WPF 来为您执行此操作。

Your HTCmds object is created by WPF by this line of XAML:

<local:HTCmds x:Key="thisobj"/>

WPF has no knowledge of Unity so it does not know how to resolve the dependencies using Unity. You need to resolve objects using UnityContainer.Resolve. You can't rely on WPF to do this for you.

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