访问视图模型类中的统一容器

发布于 2024-09-24 19:55:53 字数 2212 浏览 8 评论 0原文

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

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

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 的内容:

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

视图模型,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) 并让它加载 在 shell 中查看 (MyModuleView) 初始化时

  • 为模块创建视图模型 (MyModuleVM) 并将其设置为 显示的视图的 DataContext shell

  • MyModuleVM 包含一个命令 MyModuleView 中的按钮绑定到。 当按钮被点击时 命令被触发

  • 现在,这就是我陷入困境的地方。使用 WindowRegionAdapter(适配器 这有助于创建视图 单独的窗口)我想创建 并显示一个新视图。正如所见 MyModuleVM、LaunchTestView 需要 访问容器以便 将视图添加到区域。我怎么样 应该到达容器吗?

除了有关访问容器的具体问题之外,我将“小部件”添加到工具栏外壳并启动的总体策略是如何的 单击时的视图?当谈到 MVVM 与 Prism 时,我是否完全偏离了轨道?

谢谢你们。

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);
  }
}

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>

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;
    }
  }

So my plan was as follows:

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

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

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

  • 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?

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?

Thanks guys.

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

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

发布评论

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

评论(1

独守阴晴ぅ圆缺 2024-10-01 19:55:53

您可以通过构造函数注入容器或属性注入。为此,必须解析 ViewModel 实例 通过容器或 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.

I hope this helps.

Thanks,
Damian

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