【发布时间】:2013-09-05 06:59:51
【问题描述】:
全部 -
我在我的 DI 的 WPF 应用程序中使用 Unity(不带棱镜)。我有我的 MainWindow.xaml 和 MainWindowViewModel.cs。我的 Mainwindow.xaml 中有一个用户控件。用户控件有自己的 uc1.xaml 和 uc1viewmodel.cs。 UC1 ViewModel 当前作为 MainWindowViewModel 上的属性公开,因此我可以在用户控件上设置数据上下文(如许多 ppl 推荐的那样)。
我的问题是如何/在哪里可以设置此属性 - 它是在 app.xaml.cs 中还是在 mainwindowviewmodel 的构造函数中。代码片段:
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//Step 1 - One Time - Creating an instance of the container
UnityContainer unity = new UnityContainer();
//Step 2 - Registering your MainWindowViewModel
unity.RegisterType<IViewModel, UserControl1ViewModel>();
//Step 3 - Creating an Instance
UserControl1ViewModel uc1_mwvm = unity.Resolve<UserControl1ViewModel>(); <-- doesnt help
MainWindowViewModel mwvm = unity.Resolve<MainWindowViewModel>();
MainWindow mw = unity.Resolve<MainWindow>();
mw.Show();
}
MainWindowViewModel.cs
public class MainWindowViewModel
{
public IViewModel IVM { get; set; }
public MainWindowViewModel()
{
//IVM = new UserControl1ViewModel(); <-- All I really want is an equivalent but letting Unity do the work.
}
}
MainWindow.xaml
<Window x:Class="_05_ViewFist_UC_Unity_Working.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc1="clr-namespace:_05_ViewFist_UC_Unity_Working"
xmlns:uc2="clr-namespace:_05_ViewFist_UC_Unity_Working"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding NNN}" />
<uc1:UC1 DataContext="{Binding UC1VM}" />
<uc2:UC2 DataContext="{Binding UC2VM}" />
</StackPanel>
</Window>
UC1
<UserControl x:Class="_05_ViewFist_UC_Unity_Working.UC1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<StackPanel Orientation="Horizontal" Background="Red">
<TextBlock Text="UC1 " />
<TextBlock Text="{Binding FirstName}" />
</StackPanel>
</UserControl>
正如您从代码中看到的 - UC1 的实例是在 xaml (MainWindow.xaml) 中创建的,因此当 MainWindow 实例是在 app.xaml.cs 中创建时 - 它仍然不会创建 UserControl1ViewModel 的实例。
问题又是:不要认为我在 MainwindowViewModel 的构造函数中调用 Unity Resolve 语句是一个好习惯。那是对的吗??
有人可以分享我如何/在哪里可以做到这一点的代码 sn-p 吗?
谢谢
【问题讨论】:
-
在 MainWindowViewModel 的构造函数中设置属性。不要使用 App.xaml.cs。
-
Ninja - 在这种情况下 - 我不必在此处传递 Unity 的实例(或在此处创建另一个实例)。我记得在某处读到它是单身,但不确定这是否是正确的方法。 UnityContainer unity = new UnityContainer(); IVM = unity.Resolve
();尽管正如您所建议的那样,这可行 -
首先,unity 是用于依赖注入的。如果您不知道那是什么,请远离它。只是不要使用它并在构造函数中设置你的属性。如果您仍想使用 unity,那么您将不得不在 MainWindowViewModel 中的属性上方使用 [Dependency] 之类的属性,以便 unity 知道如何进行注入。
-
Ninja - 我非常了解 DI 是什么,而 Unity 是实现它的方法之一。同意我的属性上方没有 [Dependancy],因为我试图让这里的代码更短。
-
那么您可能知道,一旦您调用 unity.Resolve(MainWindow),UnityContainer 就会运行它的寄存器并注入您在 MainWindow 中指定的依赖项。如果您在代码中完成了我们看不到的所有正确操作,因为您希望在此处保持简短:) 那么 UnityContainer 将在调用 MainWindow 的构造函数后设置您的属性。而且你不需要自己设置任何东西。顺便说一句,在我的示例中,MainWindow 只是一个示例类。
标签: wpf unity-container