【问题标题】:Silverlight Prism fails to resolve modules on load but not alwaysSilverlight Prism 在加载时无法解析模块,但并非总是如此
【发布时间】:2012-09-27 22:46:58
【问题描述】:

我正在使用 prism 4.1 和 mef 来开发前台应用程序,但我遇到了问题。有时在模块加载过程中,应用程序会因异常而失败

无法加载文件或程序集“Sl.Common.Model,版本 = 1.0.0.0,文化 = 中性, PublicKeyToken = null" 或其依赖项之一。找不到指定的文件。

模块有依赖关系(AuditModule 和 LoadersModule 依赖于 CommonModule)。

<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                          xmlns:sys="clr-namespace:System;assembly=mscorlib" 
                          xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">

<!-- language: lang-xml -->    
    <Modularity:ModuleInfo  Ref="Sl.Common.Model.xap" InitializationMode="WhenAvailable" ModuleName="CommonModule"/>
    <Modularity:ModuleInfo Ref="Sl.Loaders.xap" ModuleName="LoadersModule">
        <Modularity:ModuleInfo.DependsOn>
            <sys:String>CommonModule</sys:String>
        </Modularity:ModuleInfo.DependsOn>
    </Modularity:ModuleInfo>
    <Modularity:ModuleInfo Ref="Sl.Audit.xap" ModuleName="AuditModule">
        <Modularity:ModuleInfo.DependsOn>
            <sys:String>CommonModule</sys:String>
        </Modularity:ModuleInfo.DependsOn>
    </Modularity:ModuleInfo>

</Modularity:ModuleCatalog>

应用程序有时可以正常启动,但是当 AuditModule 或 LoadersModule 因“CommonModule”而无法解析时出现问题,上面描述了异常。

public class Bootstrapper : MefBootstrapper
{
    private const string ModuleCatalogUri = "/ModerationSlUserInteface;component/ModulesCatalog.xaml";

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StackPanelRegionAdapter).Assembly));
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        var catalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri(ModuleCatalogUri, UriKind.Relative));
        return catalog;
    }

    protected override DependencyObject CreateShell()
    {
        return this.Container.GetExportedValue<Shell>();
    }

    protected override void InitializeShell()
    {

        //base.InitializeShell();
        Application.Current.RootVisual = (UIElement)this.Shell;
    }

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
        var stackPanelAdapterInstance = ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>();
        mappings.RegisterMapping(typeof(StackPanel), stackPanelAdapterInstance);
        return mappings;
    }
}

代码可取自here

附:当我在 Shell.xaml.cs 中点击断点时 if (e.ModuleInfo.ModuleName == LoadersModuleName)

如果第一个模块是 CommonModule 则应用程序启动正常。否则它会例外。

[Export]
public partial class Shell : UserControl, IPartImportsSatisfiedNotification
{
    private const string LoadersModuleName = "LoadersModule";
    private static Uri LoadersViewUri = new Uri("/LoadersView", UriKind.Relative);

    public Shell()
    {
        this.InitializeComponent();
    }

    [Import(AllowRecomposition = false)]
    public IModuleManager ModuleManager;

    [Import(AllowRecomposition = false)]
    public IRegionManager RegionManager;

    public void OnImportsSatisfied()
    {
        this.ModuleManager.LoadModuleCompleted += (s, e) =>
        {
            if (e.ModuleInfo.ModuleName == LoadersModuleName)
            {
                this.RegionManager.RequestNavigate(RegionNames.MainRegion, LoadersViewUri);
            }
        };
    }
}

【问题讨论】:

    标签: c# silverlight prism


    【解决方案1】:

    我们发现您还必须将模块中的任何引用添加到入口点项目。

    因此,如果您在AuditModule 中引用Microsoft.Practices.Prism.UnityExtensions(例如),那么您还需要在MainProject 中添加对它的引用,即使MainProject 中的任何代码都没有直接引用它。

    【讨论】:

    • 谢谢。我已经使它与在启动应用程序中引用 CommonModule 一起工作。但是以前怎么可能呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多