本文介绍了棱镜4 - 局部范围RegionManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Silverlight 4的应用与PRISM 4,我使用MEF。

I have silverlight 4 application with PRISM 4, I'm using MEF.

我的壳定义了模块被加载一个主区域,我想模块有自己Regi​​onManager,使得它们限定区域在本地RegionManager代替全球位置。我想这个地方RegionManager由容器(类型IRegionManager)来解决时,模块内。

My Shell defines one main region in which modules are loaded, I want modules to have their own RegionManager, so regions that they define are places in local RegionManager instead of global. And I want this local RegionManager to be resolved by container (for type IRegionManager) when inside the module.

不过从文档的方法:

IRegion detailsRegion = this.regionManager.Regions["DetailsRegion"];
View view = new View();
bool createRegionManagerScope = true;
IRegionManager detailsRegionManager = detailsRegion.Add(view, null, 
                            createRegionManagerScope);



我不工作,从子视图内解决IRegionManager时,我仍然得到GlobalRegionManager。

Doesnt work for me, when resolving IRegionManager from inside child view I still get GlobalRegionManager.

推荐答案

我是在相同的情况下,你,我有一个嵌套的区域经理,但所有的子观点仍然得到全球区域经理。我想出了什么,我认为一个合理的解决方案。

I was in the same situation as you, I had a nested region manager, but all of the child views were still getting the global region manager. I came up with what I consider a reasonable solution.

首先,我定义一个接口:

First I define an interface:

/// <summary>
/// An interface which is aware of the current region manager.
/// </summary>
public interface IRegionManagerAware
{
    /// <summary>
    /// Gets or sets the current region manager.
    /// </summary>
    /// <value>
    /// The current region manager.
    /// </value>
    IRegionManager RegionManager { get; set; }
}



然后我设置一个 RegionBehavior 像这样:

/// <summary>
/// A behaviour class which attaches the current scoped <see cref="IRegionManager"/> to views and their data contexts.
/// </summary>
public class RegionAwareBehaviour : RegionBehavior
{
    /// <summary>
    /// The key to identify this behaviour.
    /// </summary>
    public const string RegionAwareBehaviourKey = "RegionAwareBehaviour";

    /// <summary>
    /// Override this method to perform the logic after the behaviour has been attached.
    /// </summary>
    protected override void OnAttach()
    {
        Region.Views.CollectionChanged += RegionViewsChanged;
    }

    private void RegionViewsChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Contract.Requires<ArgumentNullException>(e != null);

        if (e.NewItems != null)
            foreach (var item in e.NewItems)
                MakeViewAware(item);
    }

    private void MakeViewAware(object view)
    {
        Contract.Requires<ArgumentNullException>(view != null);

        var frameworkElement = view as FrameworkElement;
        if (frameworkElement != null)
            MakeDataContextAware(frameworkElement);

        MakeAware(view);
    }

    private void MakeDataContextAware(FrameworkElement frameworkElement)
    {
        Contract.Requires<ArgumentNullException>(frameworkElement != null);

        if (frameworkElement.DataContext != null)
            MakeAware(frameworkElement.DataContext);
    }

    private void MakeAware(object target)
    {
        Contract.Requires<ArgumentNullException>(target != null);

        var scope = target as IRegionManagerAware;
        if (scope != null)
            scope.RegionManager = Region.RegionManager;
    }
}



然后将此运用到你的引导程序的所有国家和地区:

Then apply this to all regions in your bootstrapper:

protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
    var behaviours = base.ConfigureDefaultRegionBehaviors();

    behaviours.AddIfMissing(RegionAwareBehaviour.RegionAwareBehaviourKey, typeof(RegionAwareBehaviour));

    return behaviours;
}



然后,所有你需要做的是落实 IRegionManagerAware 在您的视图/视图模型,大概像这样:

Then all you have to do is implement IRegionManagerAware on your view/viewmodel, probably like so:

public class MyView : IRegionManagerAware
{
    IRegionManager RegionManager { set; get; }
}

当此视图添加到区域

然后,行为将正确地设置在 RegionManager 属性设置为当前范围区域经理。

Then when this view is added to a region, the behaviour will correctly set the RegionManager property to the currently scoped region manager.

这篇关于棱镜4 - 局部范围RegionManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 04:43