Prism 中的两个 RegisterViewWithRegion 重载有什么区别 [英] What's the difference between the two RegisterViewWithRegion overloads in Prism

查看:38
本文介绍了Prism 中的两个 RegisterViewWithRegion 重载有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Prism4,并且在我的一个模块中,我尝试将视图注册到一个区域并处理其按钮单击事件(当用户单击视图上的按钮时发布).

I'm using Prism4 and in one of my modules I'm trying to register a view with a region and also handle its button clicked event (which is published when user clicks on a button on the view).

public class MyModule : IModule
{
    private readonly IUnityContainer container;
    private readonly IRegionManager regionManager;
    private readonly IEventAggregator eventAggregator;

    public MyModule(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
    {
        this.container     = container;
        this.regionManager = regionManager;
        this.eventAggregator = eventAggregator;

        eventAggregator.GetEvent<ViewAButtonClicked>().Subscribe(ViewAButtonClicked);
    }

    public void Initialize()
    {
        this.regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewA));
        
        // this 2nd overload would work fine
        //this.regionManager.RegisterViewWithRegion("MainRegion", () => this.container.Resolve<ViewA>());            
    }

    public void ViewAButtonClicked()
    {
       // some handling code 
       // does *NOT* execute if using the 1st RegisterViewWithRegion overload
       // executes if using the 2nd RegisterViewWithRegion overload
    }
}

除了不执行 ViewAButtonClicked 方法之外,上面的代码工作正常.

The above code works fine up except that it does not execute the ViewAButtonClicked method.

如果我切换到使用其他重载(在上面的代码中注释掉),那么一切都按预期工作,ButtonClicked 方法会运行.

If I switched to use the other overload (commented out in code above) then everything works as expected, the ButtonClicked method runs.

来自 msdn 的描述非常相似,我不确定为什么它给了我上述不同的行为.为什么在处理按钮点击事件时一个有效而一个无效?

The descriptions from msdn are very similar and I'm not sure why it's giving me the different behavior described above. Why does one work and one doesn't when it comes to handling the button click event?

RegisterViewWithRegion(IRegionManager, String, Func<(Of <(Object>)>)):将视图与区域关联,使用委托来解析 ?看法.当 region get 显示时,会调用这个 delegate,并将结果添加到 region 的 views 集合中.

RegisterViewWithRegion(IRegionManager, String, Func<(Of <(Object>)>)): Associate a view with a region, using a delegate to resolve a concreate instance of the ? view. When the region get's displayed, this delelgate will be called and the result will be added to the views collection of the region.

RegisterViewWithRegion(IRegionManager, String, Type):通过注册类型将视图与区域关联.当显示区域时,此类型将使用 ServiceLocator 解析为具体实例.该实例将被添加到该区域的 Views 集合中.

RegisterViewWithRegion(IRegionManager, String, Type): Associate a view with a region, by registering a type. When the region get's displayed this type will be resolved using the ServiceLocator into a concrete instance. The instance will be added to the Views collection of the region.

推荐答案

您可能遇到的问题是,使用 typeof 方法时,Module 类正在被 GC,因此没有订阅者可用.

The problem you are probably having is that with the typeof approach, the Module class is being GCed, and thus no subscriber is available.

使用另一种方法时,当您生成 lambda 以在区域中注册视图时,Module 类应该挂起.

With the other approach, as you are generating a lambda to register the view in the region, the Module class should be hanging around.

事件聚合器可以选择使引用保持活动状态(不使用弱引用),这应该可以解决问题.

The event aggregator has an option to keep a reference alive (without using a weak reference), which should do the trick.

这篇关于Prism 中的两个 RegisterViewWithRegion 重载有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆