Silverlight PRISM,显示/隐藏模块问题

发布于 2024-08-05 02:25:51 字数 349 浏览 3 评论 0原文

我使用 PRISM 框架创建了一个项目。我有一些模块可以在 shell 中注册和显示,并且效果很好。我需要做的是当从事件聚合器接收到特定事件时动态显示和隐藏模块。

我触发了一个应该接收的 DisplayModule 事件并隐藏现有模块并显示一个新模块,但我的问题是我应该在哪里接收此事件并显示该模块?外壳?如果是这样,我将如何执行此操作,因为 shell 是顶层并且似乎没有继承所需的 Unity 方法。

我还认为我可以有一个单独的模块来侦听 DisplayModule 事件并使用区域管理器隐藏前一个模块并显示新模块。但这需要引用所有其他模块才能解析它们的类型,这是不好的做法吗?

希望这是有道理的。

谢谢您的宝贵时间

I have created a project using the PRISM framework. I have a few modules that I can register and display in the shell and this works fine. What I need to do is dynamically display and hide the modules when a particular event is received from the event aggregator.

I fire off a DisplayModule event that should be received and hide an existing module and show a new one, but my question is where is the place I should receive this event and display the module? the shell? if so, how would I do this as the shell is the top level and doesn't seem to inherit the required Unity methods.

I also thought I could have a seperate module that listened for the DisplayModule event and used the region manager to hide the previous module and show the new one. This would need to reference all other modules to be able to resolve their type though, is this bad practice?

Hope that makes sense.

Thankyou for your time

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

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

发布评论

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

评论(2

明天过后 2024-08-12 02:25:51

不过,您无法卸载整个模块。您可以动态加载一个,但不能卸载一个。有关详细信息,请阅读以下文章中的按需加载模块http://msdn.microsoft.com/en-us/library/dd458911.aspx

假设:我将从假设开始,当你说你想要要卸载模块,您实际上只想从应用程序中删除其所有视图。

首先,我们来谈谈模块如何工作以及它们如何在 shell 上显示其内容。

您的 shell 将具有一个或多个区域,您的模块可以在初始化时向其注册视图。

public MyFirstModule : IModule
{
     IRegionManager _mgr;
     public MyFirstModule(IRegionManager mgr)
     {
          _mgr = mgr;
     }

     public void Initialize()
     {
          _mgr.RegisterViewWithRegion("MainRegion", typeof(MyView));
     }

}

不过,您可以做的是更改模块初始化以跟踪已在此模块中注册的视图并在适当时卸载它们。

public MyFirstModule : IModule
{
     IRegionManager _mgr;
     IEventAggregator _agg;
     IUnityContainer _container;
     public MyFirstModule(IRegionManager mgr, 
                          IEventAggregator agg, 
                          IUnityContainer container)
     {
          _mgr = mgr;
          _agg = agg;
          _container = container;
     }

     List<object> Views = new List<object>();
     public void Initialize()
     {
          _mgr.RegisterViewWithRegion("MainRegion", () =
          {
               MyView view = _container.Resolve<MyView>();

               //Track this view so I can remove it if needed
               Views.Add(view);
               return view;
          });

          _agg.GetEvent<UnloadModuleRequest>.Subscribe(RemoveViews,                
                                                       ThreadOptions.UIThread);
     }

     private void RemoveViews(string moduleToUnload)
     {
          //If this is true, that means the Shell is asking
          //me to unload, so I will remove any views I've
          //registered from any regions they were registered to
          if(moduleToUnload == "MyFirstModule")
          {
               foreach(object view in Views)
               {
                    _mgr.Regions["MainRegion"].Remove(view);
               }
          }

     }

}

这样,您的 Shell 就可以发布名为 UnloadModuleRequest 的事件(类型为 CompositePresentationEvent),并让任何监听的模块卸载其已注册的任何视图。

我希望这接近您想要的。

You can't unload an entire module, though. You can dynamically load one, but not unload one. For more information read Loading Modules on Demand in the following article: http://msdn.microsoft.com/en-us/library/dd458911.aspx

Assumption: I'm going to start off with the assumption that when you say you want to unload a module, you really just want all of its views to be removed from your application.

First off, let's talk about how your modules work and how they show their content on your shell.

Your shell will have one or more Regions that your modules can then register views with when they are initialized.

public MyFirstModule : IModule
{
     IRegionManager _mgr;
     public MyFirstModule(IRegionManager mgr)
     {
          _mgr = mgr;
     }

     public void Initialize()
     {
          _mgr.RegisterViewWithRegion("MainRegion", typeof(MyView));
     }

}

What you can do, though, is change your module initialization to track views that have been registered with this module and unload them when appropriate.

public MyFirstModule : IModule
{
     IRegionManager _mgr;
     IEventAggregator _agg;
     IUnityContainer _container;
     public MyFirstModule(IRegionManager mgr, 
                          IEventAggregator agg, 
                          IUnityContainer container)
     {
          _mgr = mgr;
          _agg = agg;
          _container = container;
     }

     List<object> Views = new List<object>();
     public void Initialize()
     {
          _mgr.RegisterViewWithRegion("MainRegion", () =
          {
               MyView view = _container.Resolve<MyView>();

               //Track this view so I can remove it if needed
               Views.Add(view);
               return view;
          });

          _agg.GetEvent<UnloadModuleRequest>.Subscribe(RemoveViews,                
                                                       ThreadOptions.UIThread);
     }

     private void RemoveViews(string moduleToUnload)
     {
          //If this is true, that means the Shell is asking
          //me to unload, so I will remove any views I've
          //registered from any regions they were registered to
          if(moduleToUnload == "MyFirstModule")
          {
               foreach(object view in Views)
               {
                    _mgr.Regions["MainRegion"].Remove(view);
               }
          }

     }

}

That way your Shell can publish an event called UnloadModuleRequest (of type CompositePresentationEvent<string>) and have any module listening unload any views it has registered.

I hope this is close to what you are wanting.

红衣飘飘貌似仙 2024-08-12 02:25:51

以下线程处理类似的情况并解释实现目标的可能方法:

请告诉我这是否有帮助。

达米安·申克尔曼
http://blogs.southworks.net/dschenkelman

The following threads deal with similar situations and explain possible ways of achieving your goals:

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

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