Access to the unity container in the view of the model class

I have a wrapper that looks like a toolbar and defines my main area (wrapper panel). What I need to do is the ability to add widgets to the shell, and when I click the widget a new window (view) opens. Below I still:

I created a module class that adds a view to the main area:

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>

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 a class that implements IModule (MyModule) and load view (MyModuleView) into the shell when initializing

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

  • MyModuleVM , MyModuleView.

  • . WindowRegionAdapter ( ) . MyModuleVM, LaunchTestView . ?

, "" ? , MVVM Prism?

, .

+3
1

, . ViewModel BuildUp , .

, .

, Damian

+1

Source: https://habr.com/ru/post/1766268/


All Articles