Wpf 运行目录(prism)的所有模块
我正在使用prism的桌面库 我想要的是在一个目录中获取模块,然后运行它们 我喜欢这样:Wpf 运行目录(prism)的所有模块,wpf,module,prism,modularity,Wpf,Module,Prism,Modularity,我正在使用prism的桌面库 我想要的是在一个目录中获取模块,然后运行它们 我喜欢这样: DirectoryModuleCatalog catalog = new DirectoryModuleCatalog(); catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules"; 我检查过了,模块已加载到目录中。 模块示例: public cl
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";
我检查过了,模块已加载到目录中。
模块示例:
public class SendEmailClass : IModule
{
public void SendEmail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("**", "moi");
mail.Subject = "Report"; //manage generated subject
mail.To.Add("***");
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.Port = 57;
smtp.EnableSsl = true; //depending of the smtp server
NetworkCredential cred = new NetworkCredential("***", "***");
smtp.Credentials = cred;
smtp.Send(mail);
}
public void Initialize()
{
SendEmail();
}
}
但是我想运行它们(启动它们的Initialize()),但找不到它。我想运行整个目录。有人有主意吗?我尝试了catalog.Initialize()、catalog.Validate()或catalog.Load()您的代码看起来是正确的,我的印象是您必须重写
引导程序
类中的GetModuleCatalog()
方法才能做到这一点。下面是一个非常简单的引导程序的工作示例,它从模块目录加载模块
public class Bootstrapper : UnityBootstrapper
{
private const string MODULE_FOLDER = @".\modules";
protected override IModuleCatalog GetModuleCatalog()
{
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog() { ModulePath = MODULE_FOLDER };
return catalog;
}
}
更新
可能不需要使用引导程序来加载模块,但我不明白您为什么不利用UnityBootstrapper
类,它可以为您完成这项工作
Bootstrapper bootStrapper = new Bootstrapper();
bootStrapper.Run();
您的模块将被加载。我自己从来没有在不使用引导程序的情况下这样做过,因为它非常简单。正如jsmith所说,默认引导程序为您自己执行的配置提供了繁重的工作。如果没有Shell,只需创建一个跳过该部分的自定义引导程序
如果您不想以任何方式使用引导程序,您可以实例化该类,将ModuleCatalog作为参数之一传递,并调用ModuleManager的方法
正如我之前所说的,以上是由引导程序为您完成的。
我希望这有帮助
谢谢,
Damian您如何引导应用程序?您是否创建了引导程序类?是引导程序初始化了所有模块
protected override void OnStartup(StartupEventArgs e)
{
ApplicationBootstrapper bootstrapper = new ApplicationBootstrapper();
bootstrapper.Run();
}
这是我制作的引导程序类的一个示例
public class ApplicationBootstrapper : UnityBootstrapper
{
// Here is where you create your module catalog
protected override IModuleCatalog GetModuleCatalog()
{
DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";
return catalog;
}
// Here is where you create your user interface shell.
protected override DependencyObject CreateShell()
{
Container.RegisterInstance<IApplicationSettings>(new ApplicationSettings());
Shell shell = Container.Resolve<Shell>();
if (shell != null)
shell.Show();
return shell;
}
}
本例使用的是Unity引导程序,您所需要的只是可与之并行使用的。是否可以不使用引导程序?实际上我不使用任何shell,我将在windows服务中使用它(没有gui)。谢谢您的回答。但是,如果我想每15分钟加载一次模块(使用计时器),我想我不能使用引导程序,因为它只在应用程序开始时加载,对吗?@raphael:模块不应该加载很多次。它们只需加载一次(就像在appdomain中加载程序集一样),并且它们的组件可以重用。更多信息:好的!因此,我理解得很清楚:如果在我的目录“模块”中添加一个新的模块(DLL),我需要重新启动应用程序来考虑这个新的模块,而不是必要的。如果要在应用程序运行时加载模块,可以使用按需加载模块()。使用FileSystemWatcher并在目录中注册模块应该可以实现您的目标。Prism在默认情况下不提供此功能(监视目录并在目录更改时自动加载模块),但只要对目录目录模块加载程序进行几次更改,就可以了。