在视图模型类中访问统一容器 [英] Accessing unity container in view model class

查看:97
本文介绍了在视图模型类中访问统一容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外壳,看起来像工具栏,并定义了我的主要区域(环绕面板).我需要做的是能够将小部件添加到外壳中,并且当单击小部件时,将打开一个新窗口(视图).以下是我到目前为止的内容:

I have a shell which looks like toolbar and defines my main region (a wrap panel). What I need to do is be able to add widgets to the shell and when a widget is clicked, a new window (view) is opened. Below is what I have so far:

我创建了一个模块类,该模块将视图添加到主要区域:

I created a module class which adds a view to the main region:

public class MyModule : IModule
{
  protected IUnityContainer Container { get; private set; }

  public MyModule(IUnityContainer container)
  {
    Container = container.CreateChildContainer();
  }

  public void Initialize()
  {
    var regionManager = Container.Resolve<IRegionManager>();
    MyModuleView myModuleView = new MyModuleView();
    regionManager.Regions["MainRegion"].Add(myModuleView);
  }
}

这是MyModuleView的内容:

Here is the content of MyModuleView:

  <Grid>
    <Grid.DataContext>
      <vm:MyModuleVM/>
    </Grid.DataContext>
    <Button Content="My Module" Foreground="White" FontWeight="Bold" Command="{Binding Path=LaunchCommand}">
    </Button>
  </Grid>

视图模型MyModuleVM:

The view model, MyModuleVM:

  class MyModuleVM : ObservableObject
  {
    protected IUnityContainer Container { get; private set; }

    public MyModuleVM()
    {
    }

    RelayCommand _launchCommand;
    public ICommand LaunchCommand
    {
      get
      {
        if (_launchCommand == null)
        {
          _launchCommand = new RelayCommand(() => this.LaunchTestView(),
              () => this.CanLaunchTestView());
        }
        return _launchCommand;
      }
    }

    private void LaunchTestView()
    {
      TestView view = new TestView();
      view.Title = "Test View";
      var regionManager = Container.Resolve<IRegionManager>();
      regionManager.Regions["MyWindowRegion"].Add(view);
    }

    private bool CanLaunchTestView()
    {
        return true;
    }
  }

所以我的计划如下:

  • 创建实现的类 IModule(MyModule)并加载 视图(MyModuleView)进入外壳 初始化时

  • Create the class that implements IModule (MyModule) and have it load a view (MyModuleView) into the shell when initialized

为模块创建视图模型 (MyModuleVM)并将其设置为 显示在中的视图的DataContext 外壳

Create a view model for the module (MyModuleVM) and set it as the DataContext of the view displayed in the shell

MyModuleVM包含以下命令: MyModuleView中的按钮绑定到. 单击按钮后, 命令被触发

MyModuleVM contains a command that a button in MyModuleView binds to. When the button is clicked the command is triggered

现在,这是我被困住的地方.使用 WindowRegionAdapter(适配器 这有助于在其中创建视图 独立的窗口)我想创建 并显示一个新视图.如所见 MyModuleVM,LaunchTestView需要 进入容器以 将视图添加到区域.我怎么样 应该去集装箱吗?

Now, here is where I am stuck. Using a WindowRegionAdapter (an adapter that helps to create views in separate windows) I wanted to create and display a new view. As seen in MyModuleVM, LaunchTestView needs access to the container in order to add the view to a region. How am I supposed to get to the container?

除了我关于访问容器的特定问题外,我向工具栏"外壳添加小部件"并启动的总体策略如何? 单击它们时的视图?在使用Prism进行MVVM时,我是否完全偏离了轨道?

Besides my specific question about accessing the container, how is my overall strategy of adding "widgets" to a toolbar shell and launching views when they are clicked? Am I comlpetely off track here when it comes to MVVM with Prism?

谢谢大家.

推荐答案

您可以通过已解决通过容器或 BuildUp 方法应该在实例化之后调用它.

You can get the container injected through constructor or property injection. To do that, the ViewModel instance must be resolved by the container, or the BuildUp method should be called after it has been instantiated.

我希望这会有所帮助.

谢谢, 达米安

这篇关于在视图模型类中访问统一容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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