Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 设置UserControl ViewModel属性_Wpf_Unity Container - Fatal编程技术网

Wpf 设置UserControl ViewModel属性

Wpf 设置UserControl ViewModel属性,wpf,unity-container,Wpf,Unity Container,所有- 我在我的WPF DI应用程序中使用Unity(没有prism)。我有MainWindow.xaml和MainWindowViewModel.cs。我的Mainwindow.xaml中有一个usercontrol。用户控件有自己的uc1.xaml和uc1viewmodel.cs。UC1 ViewModel当前作为MainWindowViewModel上的属性公开,因此我可以在usercontrol上设置datacontext(正如许多ppl推荐的那样) 我的问题是如何/在哪里设置此属性-它

所有-

我在我的WPF DI应用程序中使用Unity(没有prism)。我有MainWindow.xaml和MainWindowViewModel.cs。我的Mainwindow.xaml中有一个usercontrol。用户控件有自己的uc1.xaml和uc1viewmodel.cs。UC1 ViewModel当前作为MainWindowViewModel上的属性公开,因此我可以在usercontrol上设置datacontext(正如许多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();
    }
启动时受保护的覆盖无效(StartupEventArgs e)
{
基础。启动时(e);
//步骤1-一次-创建容器的实例
UnityContainer unity=新的UnityContainer();
//步骤2-注册MainWindowViewModel
unity.RegisterType();
//步骤3-创建实例

UserControl1ViewModel uc1_mwvm=unity.Resolve();您可以使用服务定位器模式。我将其与unity一起用作DI

internal class ServiceLocator
{
    [...]
    public MainViewModel Main { get { return container.Resolve<MainViewModel>(); } }
}
编辑:

我找到了另一种方法(其中包括):
查看。在命令中,您可以根据需要解析viewmodel。

我从github下载了您的解决方案,并尝试解决您的问题

你做得很好,只是忘记了一些细节,比如属性

以下是您的App.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 - Registeration
        unity.RegisterType<IMainWindowViewModel, MainWindowViewModel>();
        unity.RegisterType<IUC1ViewModel, UC1ViewModel>();
        unity.RegisterType<IUC2ViewModel, UC2ViewModel>();


        //// Instance of MainWindowViewModel will be created once you call Resolve MainWindow.


        MainWindow mw = unity.Resolve<MainWindow>();

        mw.Show();
    }
[Dependency]
是一个属性属性属性,它告诉Unity在何处注入值

如果您愿意,我可以将我的代码合并到您在github的回购协议中


让我知道这是否对您有任何帮助。请随意将其标记为答案。

在MainWindowViewModel的构造函数中设置属性。不要使用App.xaml.cs.Ninja-在这种情况下-我不必在此处传递Unity实例(或在此处创建另一个实例).我记得读过一些文章,其中是一个单例,但不确定这是否是正确的方法。UnityContainer unity=new UnityContainer();IVM=unity.Resolve();这是可行的,但正如您所建议的,首先unity用于依赖项注入。如果您不知道这是什么,请远离它。不要使用它,并在构造函数中设置属性。如果您仍要使用unity,则必须使用像[依赖项]这样的属性在MainWindowViewModel中添加属性,这样unity就知道如何进行注入。忍者-我非常了解DI是什么,unity是实现它的方法之一。同意我没有[依赖性]在我的属性上面,因为我试图使这里的代码更短。那么你应该知道,一旦你调用unity.Resolve(主窗口)UnityContainer运行其寄存器并注入您在MainWindow中指定的依赖项。如果您在代码中完成了我们无法看到的所有正确操作,因为您希望在此处保持简短:),则UnityContainer将在调用MainWindow的构造函数后设置您的属性。您不需要设置任何内容rself.Btw在我的示例中,MainWindow只是一个示例类。Daniel-这里的问题不是实例化我的MainWindowViewModel类,而是在我的MWVM中实例化我的UserControl1ViewModel,以便我可以设置绑定。我相信您给出的解决方案是实例化MainWindowViewModel类的模式之一(我已经通过构造函数注入进行了这项工作)它与您的usercontrol完全相同。如果您喜欢,我可以重命名它-让我尝试返回。无需重命名:)更多地了解这一点,可以看到很多文章都说不要在DI中使用Locater模式。这里有一篇重要的文章——情况并不完全相同。在这里,我们使用服务定位器仅从视图中解析视图模型一次。我们在代码中不提供解析类的通用方法。Ninja/Dev-非常感谢您的帮助我希望我能完成这项工作。在我的示例解决方案中尝试了这项工作,效果很好。这正是我从方法角度所寻找的。我知道你提到过[依赖性]属性,但直到你给我看了上面的代码片段,我才明白。我真的很感谢你解决了下载的问题并帮助我来到这里。我已经将此标记为一个答案并给你一个投票:)顺便问一下-这是你的企业吗???hhogdev.com,如果你可以向我发送你的电子邮件添加ress-想通过电子邮件交谈/聊天关于DI/WPF的几件事-不是问题:)我的电子邮件地址在我的个人资料中列出(公开)嗨,不,那不是我的事,呵呵。我们很少有人称自己为刺猬的。我不知道我们是怎么想出刺猬的东西的,但是有忍者刺猬,我是开发刺猬,还有武士刺猬等等。我的邮件是艾迪devhedgehog@gmail.com告诉我你有什么wpf问题。:)哇-我以为你和忍者是一样的:):。谢谢你的电子邮件地址-很快就会给你留言
<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>
internal class ServiceLocator
{
    [...]
    public MainViewModel Main { get { return container.Resolve<MainViewModel>(); } }
}
<Application.Resources>
        <vm:ServiceLocator x:Key="Locator"/>
    </Application.Resources>
DataContext="{Binding Main, Source={StaticResource Locator}}"
    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 - Registeration
        unity.RegisterType<IMainWindowViewModel, MainWindowViewModel>();
        unity.RegisterType<IUC1ViewModel, UC1ViewModel>();
        unity.RegisterType<IUC2ViewModel, UC2ViewModel>();


        //// Instance of MainWindowViewModel will be created once you call Resolve MainWindow.


        MainWindow mw = unity.Resolve<MainWindow>();

        mw.Show();
    }
public class MainWindowViewModel : IMainWindowViewModel
{
    #region Public Properties

    [Dependency]
    public IUC1ViewModel UC1VM { get; set; }

    [Dependency]
    public IUC2ViewModel UC2VM { get; set; }

    public string NNN { get; set; }

    #endregion

    public MainWindowViewModel()
    {
        NNN = "This value coming from MainWindowViewModel";
    }
}