WPF PRISM - 在不解析实例的情况下向区域注册视图

发布于 2024-10-08 02:22:21 字数 183 浏览 0 评论 0原文

是否可以按类型在模块初始化内的区域注册视图,而不会导致视图被解析(实例化),直到请求激活视图为止。

这种情况的一个场景是当我们采用视图优先方法并依赖于外部服务时。我不希望在用户真正需要该视图之前初始化服务,这将有助于提高性能,因为某些外部服务需要花费时间来初始化。

注意:多个视图注册到同一区域(例如菜单导航样式场景)。

Is it possible to register a view by type with a region within the Module initialization without causing the view to be resolved (instantiated) until the view is request for activation.

A scenario for this is when we have view-first approach with a dependency on and external service. I do not want the service to be initialized until the user really requires that view and this will help improve performance because some external services cost time to initialize.

Note: Multiple views are register to the same region (eg. menu navigation style scenario).

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

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

发布评论

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

评论(1

雪化雨蝶 2024-10-15 02:22:21

有两种方法可以解决这个问题。一个比另一个拥有更多的文档。

  1. 通过使用视图注入自行接管实例化,而不是允许区域实例化您的视图(称为视图发现)。这非常简单,但如果不知道您的解决方案,就很难确切地知道您在寻找什么。

  2. 利用 IActiveAware 界面。在这种情况下,您实际上允许实例化视图,但您的视图将等待来自区域管理器的视图处于活动状态的通知。下面是 IActiveAware 界面(它非常不言自明): http://msdn.microsoft.com/en-us/library/microsoft.practices.prism.iactiveaware_members(v=pandp.38).aspx

和一个小代码示例:

public class MyView : IActiveAware
{
     private bool _isActive = false;
     public bool IsActive
     {
          get { return _isActive; }
          set 
          { 
               _isActive = value;
               if(value)
               {
                     //Good idea to thread this if you can
                     DoSomethingExpensive();
               }
          }

     }

}

There are two ways to go about this. One has more documentation than the other.

  1. Take over instantiation yourself by using View Injection, rather than allowing the Region to instantiate your view (called View Discovery). This is pretty straightforward, but without knowing your solution, it'd be hard to know exactly what you are looking for.

  2. Utilize the IActiveAware interface. In this scenario, you actually allow the view to be instantiated, but your view would wait for notification from the Region Manager that the view is active. Here is a look at the IActiveAware interface (it's pretty self-explanatory): http://msdn.microsoft.com/en-us/library/microsoft.practices.prism.iactiveaware_members(v=pandp.38).aspx

And a small codesample:

public class MyView : IActiveAware
{
     private bool _isActive = false;
     public bool IsActive
     {
          get { return _isActive; }
          set 
          { 
               _isActive = value;
               if(value)
               {
                     //Good idea to thread this if you can
                     DoSomethingExpensive();
               }
          }

     }

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