如何在初始化后激活/取消激活模块的视图?

时间:2021-04-24 12:51:43

This relates to Composite Application Guidance for WPF, or Prism.

这涉及WPF或Prism的复合应用指南。

I have one "MainRegion" in my shell. My various modules will be loaded into this main region. I can populate a list of available modules in a menu and select them to load. On the click of the menu I do:

我的shell中有一个“MainRegion”。我的各种模块将被加载到这个主要区域。我可以在菜单中填充可用模块列表并选择它们进行加载。点击我做的菜单:

var module = moduleEnumerator.GetModule(moduleName);
moduleLoader.Initialize(new[] { module });

At the first time all works ok, because the Initialize() methods of the modules are executed, but after Module1, Module2 and Module3 are initialized, nothing happens when I click to load Module2 again.

第一次一切正常,因为模块的Initialize()方法被执行,但在Module1,Module2和Module3初始化之后,当我再次点击再加载Module2时没有任何反应。

My question: how can I activate a module on demand, after its initialize method has been executed?

我的问题:如何在执行初始化方法后按需激活模块?

Thank you for your help!

谢谢您的帮助!

4 个解决方案

#1


4  

You don't actually activate the module. You activate a view in a region. Take a read of this article.

您实际上并未激活该模块。您激活区域中的视图。阅读本文。

The Initialize method is only called the once for any module. The fact that you are seeing a view in the module being activated when you call LoadModule I would guess is due to the fact that the Initilalize method is registering a view with a region. This will activate the view. If you had more than one view then the last registered would be the active one.

Initialize方法仅对任何模块调用一次。您在调用LoadModule时看到模块中的视图被激活的事实我猜是因为Initilalize方法正在使用区域注册视图。这将激活视图。如果您有多个视图,则最后注册的视图将是活动视图。

To Activate a view you need to call the Activate method of the region (assuming an injected IUnityContainer and IRegionManager)...

要激活视图,您需要调用该区域的Activate方法(假设注入了IUnityContainer和IRegionManager)...

// Get a view from the container.
var view = Container.Resolve<MyView>();

// Get the region.
var region = RegionManager.Regions["MyRegion"];

// Activate the view.
region.Activate(view);

Depending on the type of region control this will either replace the view that is there or add to it.

根据区域控件的类型,这将替换那里的视图或添加到它。

#2


2  

You can remove a View by calling Regions's Remove method.

您可以通过调用Regions的Remove方法删除View。

public void RemoveViewFromRegion(string viewName, string regionName, object defaultView)
    {
      IRegion region = regionManager.Regions[regionName];
      object view = region.GetView(viewName);
      region.Remove(view);
      region.Activate(defaultView); 
    }

#3


0  

You should have a ContentControl that will be your region. Then you will need to add all your modules to this region. When you click on the menu you should use Activate(...) method of the region in order to activate the particular module.

你应该拥有一个属于你所在地区的ContentControl。然后,您需要将所有模块添加到此区域。单击菜单时,应使用区域的激活(...)方法以激活特定模块。

#4


0  

Does this mean when yuou activate module, then other modules that may be overlapped by it are set to Visibility.Collapsed?

这是否意味着当你激活模块时,那么可能与它重叠的其他模块被设置为Visibility.Collapsed?

#1


4  

You don't actually activate the module. You activate a view in a region. Take a read of this article.

您实际上并未激活该模块。您激活区域中的视图。阅读本文。

The Initialize method is only called the once for any module. The fact that you are seeing a view in the module being activated when you call LoadModule I would guess is due to the fact that the Initilalize method is registering a view with a region. This will activate the view. If you had more than one view then the last registered would be the active one.

Initialize方法仅对任何模块调用一次。您在调用LoadModule时看到模块中的视图被激活的事实我猜是因为Initilalize方法正在使用区域注册视图。这将激活视图。如果您有多个视图,则最后注册的视图将是活动视图。

To Activate a view you need to call the Activate method of the region (assuming an injected IUnityContainer and IRegionManager)...

要激活视图,您需要调用该区域的Activate方法(假设注入了IUnityContainer和IRegionManager)...

// Get a view from the container.
var view = Container.Resolve<MyView>();

// Get the region.
var region = RegionManager.Regions["MyRegion"];

// Activate the view.
region.Activate(view);

Depending on the type of region control this will either replace the view that is there or add to it.

根据区域控件的类型,这将替换那里的视图或添加到它。

#2


2  

You can remove a View by calling Regions's Remove method.

您可以通过调用Regions的Remove方法删除View。

public void RemoveViewFromRegion(string viewName, string regionName, object defaultView)
    {
      IRegion region = regionManager.Regions[regionName];
      object view = region.GetView(viewName);
      region.Remove(view);
      region.Activate(defaultView); 
    }

#3


0  

You should have a ContentControl that will be your region. Then you will need to add all your modules to this region. When you click on the menu you should use Activate(...) method of the region in order to activate the particular module.

你应该拥有一个属于你所在地区的ContentControl。然后,您需要将所有模块添加到此区域。单击菜单时,应使用区域的激活(...)方法以激活特定模块。

#4


0  

Does this mean when yuou activate module, then other modules that may be overlapped by it are set to Visibility.Collapsed?

这是否意味着当你激活模块时,那么可能与它重叠的其他模块被设置为Visibility.Collapsed?