1

我在 SilverLight 4 应用程序中使用 PRISM。我有一个问题,注册到某些区域的视图没有显示。

在启动时加载模块时,我将一些视图注册到区域,如下所示:

RegionManager.RegisterViewWithRegion("MyRegion1", typeof(IMySubView1));
RegionManager.RegisterViewWithRegion("MyRegion2", typeof(IMySubView2));

我有一个视图实现了一个名为 IMyView 的接口,其中 xaml 有两个内容控件,其区域在网格中定义,如下所示:

<ContentControl Regions:RegionManager.RegionName="MyRegion1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="0" Grid.RowSpan="1"/>
<ContentControl Regions:RegionManager.RegionName="MyRegion2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1" Grid.RowSpan="1"/>

我尝试了两种不同的方法来将视图添加到主要区域。两者都添加了视图和显示按钮等基本元素,但视图中定义的区域不会被关联的视图填充。

方法一:

object obj = _container.Resolve<IMyView>();
IRegion mainRegion = _regionManager.Regions["MainViewRegion"];
IRegionManager scoped = mainRegion.Add(obj, "test", true);
mainRegion.Activate(obj);

// Enabling the following call, it will fail saying the region MyRegion1 does not exist. Feels like it should?
// IRegion myRegion = scoped.Regions["MyRegion1"];

方法二:

object obj = _container.Resolve<IMyView>();
_regionManager.AddToRegion("MainViewRegion", obj);
_regionManager.Regions["MainViewRegion"].Activate(obj);

感觉就像在 xaml 文件中定义的区域没有被注册,因此注册的视图没有被显示。

MainViewRegion 在 shell 中的 TabControl 中定义如下:

<TabControl Margin="8,0,8,8" Regions:RegionManager.RegionName="MainViewRegion">

任何有关解决我的问题的建议将不胜感激!

4

4 回答 4

0

我面临同样的问题。这个想法是,无论出于何种原因,已创建区域的视图注入 {mainRegion.Add(obj, "test", true)} 都不会显示视图。一个对我有用的解决方法是从代码创建区域,然后注入视图。像这样的东西:

Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionManager(headerRegionContainer, _RegionManager); Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionName(headerRegionContainer, regionName);

var view = _UnityContainer.Resolve(bag.HeaderViewType); _RegionManager.Regions[regionName].Add(view); _RegionManager.Regions[regionName].Activate(view);

对我来说不幸的是,我无法以这种方式达到我的目标,但也许你可以。

问候,

莱文特

于 2010-08-06T08:17:59.933 回答
0

经过数小时的故障排除后,我发现了一些东西。

在 Composite.Presentation\Regions\RegionManager.cs 中有一个名为 IsInDesignMode 的方法。当一个区域即将被创建时,这个方法被调用,如果这个方法返回true,这个区域就不会被创建。见下文:

private static void OnSetRegionNameCallback(DependencyObject element, DependencyPropertyChangedEventArgs args)
{
    if (!IsInDesignMode(element))
    {
        CreateRegion(element);
    }
}

private static bool IsInDesignMode(DependencyObject element)
{
    // Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
    return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null
           || Application.Current.GetType() == typeof(Application);
}

当我们的silverlight 应用程序启动并且shell 中的区域被创建时,一切都很好,Application.Current 属性的类型是“MyName.Shell.App”。但是当启动后添加视图时,作为对用户输入的响应,Application.Current 类型突然变为“Application”类型,因此 IsInDesignMode 方法返回 true 并且未创建区域。

如果我删除 Application.Current 条件,一切都会按预期工作。所以问题是,我的应用程序有问题还是 prism 源代码有问题?

于 2010-08-12T08:06:09.413 回答
0

你的 _regionManager 来自哪里?你写了一个合适的 BootStrapper 吗?您需要编写一个继承自 MefBootstrapper 或 UnityBootstrapper 的类(或者如果您不使用这些 IoC/Extension 框架,则为自定义类)以便在 IoC 容器中注册所有需要的类型。

你能发布 BootStrapper 代码吗?

于 2010-08-16T06:29:53.530 回答
0

问题在 Prism 4.0 版中消失了,出现问题时我正在运行 Prism 2.2 版。

于 2010-12-06T13:45:21.027 回答