如何輸入URI以在Prism WPF中設置ModuleCatalog

[英]How to enter the URI to setup the ModuleCatalog in Prism WPF


I am getting a bit frustrated with prism. Something that should be so easy is really getting me stuck!

我對棱鏡有點沮喪。應該如此簡單的東西真的讓我陷入困境!

I am trying to load my ModuleCatalog from a file. I created the ModuleCatalog.xaml file in my shell project. In the file's properties I have removed the Custom Tool action and I have set Build Action to Resource (I also tried Content).

我正在嘗試從文件加載ModuleCatalog。我在shell項目中創建了ModuleCatalog.xaml文件。在文件的屬性中,我刪除了自定義工具操作,我已將Build Action設置為Resource(我也嘗試了內容)。

Here is the code I have:

這是我的代碼:

public class Bootstrapper : UnityBootstrapper
{
   protected override IModuleCatalog GetModuleCatalog()
   {
      var uri = new Uri("/ShellProject;component/ModuleCatalog.xaml", UriKind.Relative);
      var catalog = ModuleCatalog.CreateFromXaml(uri);
      return catalog;

      // I have also tried this:
      //return (ModuleCatalog.CreateFromXaml(
      //        new Uri("ModuleCatalog.xaml", UriKind.Relative)));
   }
   ...

When I run I get the following error:

當我運行時,我收到以下錯誤:

The IModuleCatalog is required and cannot be null in order to initialize the modules.

IModuleCatalog是必需的,並且不能為null以初始化模塊。

I am stumped. The blogs I have read and the videos I have watched seem to indicate I am doing it right.

我很難過。我讀過的博客和我看過的視頻似乎表明我做得對。

I can't think I am the only one to ever have loaded my configuration from a xaml file in wpf, but I can't find anyway around this.

我不能認為我是唯一一個從wpf中的xaml文件加載配置的人,但我無論如何都找不到。

Any help would be great!

任何幫助都會很棒!

NOTES:

筆記:

  1. I am using a WPF App. There are tons of examples on how to do this in silverlight. I need it in for WPF.
  2. 我正在使用WPF應用程序。關於如何在silverlight中執行此操作有大量示例。我需要它用於WPF。
  3. I have looked at this post and have not found it helpful for my issue: http://blogs.southworks.net/dschenkelman/2009/08/09/how-to-populate-the-module-catalog-from-xaml-in-a-wpf-application-using-the-composite-application-guidance-for-wpf-silverlight-prism-v2/
  4. 我查看了這篇文章並且沒有發現它對我的問題有幫助:http://blogs.southworks.net/dschenkelman/2009/08/09/how-to-populate-the-module-catalog-from-xaml-在-A-WPF的應用程序 - 使用 - 所述復合應用指導換WPF的Silverlight的棱鏡-V2 /

This is what I did that finally worked:

這就是我最終的工作:

protected override IModuleCatalog GetModuleCatalog()
{
  FileStream catalogStream = new FileStream(@".\ModuleCatalog.xaml",FileMode.Open);
  var catalog = ModuleCatalog.CreateFromXaml(catalogStream);
  catalogStream.Dispose();
  return catalog;
}

4 个解决方案

#1


1  

To figure out what is going on you could build the Prism projects and use the .pdb files to debug them or you could simply include said projects in your solution and debug them directly from there.

要弄清楚發生了什么,你可以構建Prism項目並使用.pdb文件來調試它們,或者你可以簡單地在解決方案中包含所述項目並直接從那里調試它們。

If this is a large solution, just try this using a basic Quickstarts and then update your solution based on your findings from debugging the QS.

如果這是一個大型解決方案,只需使用基本的快速入門嘗試此操作,然后根據調試QS的結果更新您的解決方案。

One of the huge benefits of Prism releasing its source is that in these kind of situations you can simply dig into it step by step to check where the issue is.

Prism釋放其來源的巨大好處之一是,在這種情況下,您可以一步一步地深入研究問題所在。

#2


2  

I ran into the same issue, but I was trying to do it with MEF and WPF. Documented samples are still a little weak in this area, but I managed to get it working. Looking at what you have above, it appears that you are overriding the wrong method for what you want to do. You just want to create the module catalog, not configure it. I would recommend the following code in the boostrapper for MEF or Unity users trying to do this in WPF:

我遇到了同樣的問題,但我試圖用MEF和WPF來做。記錄的樣本在這個領域仍然有點弱,但我設法讓它工作。看看你上面的內容,看來你正在為你想做的事情重寫錯誤的方法。您只想創建模塊目錄,而不是配置它。我建議在MEF或Unity用戶的boostrapper中使用以下代碼嘗試在WPF中執行此操作:

protected override IModuleCatalog CreateModuleCatalog()
{
    // MEF and Unity **BOTH** use the ModuleCatalog when configuring from a file
    return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
        System.IO.File.OpenRead("catalog.xaml"));
}

Assuming you have a "WpfModule1.dll" that uses the namespace "WpfModule"1 and has a class named "WpfModule" that inherits from "IModule" you will want the following "catalog.xaml" config file:

假設你有一個“WpfModule1.dll”使用名稱空間“WpfModule”1並且有一個名為“WpfModule”的類繼承自“IModule”,你將需要以下“catalog.xaml”配置文件:

<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">
    <Modularity:ModuleInfo Ref="file:///WpfModule1.dll" 
        ModuleName="WpfModule"
        ModuleType="WpfModule1.WpfModule, WpfModule1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        InitializationMode="WhenAvailable" />
</Modularity:ModuleCatalog>

Note you also have to change the properties on the config file to Build Action:"None" and Copy To Output Directory:"Copy if Newer". Hope that helps.

請注意,您還必須將配置文件中的屬性更改為“構建操作”:“無”和“復制到輸出目錄”:“如果較新則復制”。希望有所幫助。

#3


1  

Prism is really picky about this and you must be very precise when using ModuleCatalog.CreateFromXaml()

Prism對此非常挑剔,使用ModuleCatalog.CreateFromXaml()時必須非常精確

  • Compile the ModuleCatalog.xaml with a Build Action of Page
  • 使用Page的Build Action編譯ModuleCatalog.xaml
  • Include the Version number (typically 1.0.0.0 if you didn't change anything) in the declaration. e.g.: ModuleType="MyProj.ModuleB.ModuleBModule, MyProj.ModuleB, Version=1.0.0.0"
  • 聲明中包含版本號(如果沒有更改,通常為1.0.0.0)。例如:ModuleType =“MyProj.ModuleB.ModuleBModule,MyProj.ModuleB,Version = 1.0.0.0”

#4


1  

In our case, we were getting the same error:

在我們的例子中,我們得到了同樣的錯誤:

The IModuleCatalog is required and cannot be null in order to initialize the modules.

IModuleCatalog是必需的,並且不能為null以初始化模塊。

after updating from Prism 4.x to 5.0. It turned out to be because of the assembly reorganization -- the assembly Microsoft.Practices.Prism referenced in the ModuleCatalog.xaml no longer exists; it is now Microsoft.Practices.Prism.Composition. Easy to overlook the squiggly line in the IDE.

從Prism 4.x更新到5.0后。原來是因為程序集重組 - ModuleCatalog.xaml中引用的程序集Microsoft.Practices.Prism不再存在;它現在是Microsoft.Practices.Prism.Composition。很容易忽略IDE中的波浪線。

So for Prism 5.0, the root element in ModuleCatalog.xaml should look like this:

因此,對於Prism 5.0,ModuleCatalog.xaml中的根元素應如下所示:

<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.Composition">

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/10/24/7255830fd34e94eeb95af99d6a1d6cbe.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com