网站入口百度,单页网站源码下载,怎样做网站上的语种链接,大连甘井子区教育局1.概要源码及PPT地址#xff1a;https://github.com/JusterZhu/wemail视频地址#xff1a;https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesourcecopywebModule#xff0c;具有特定功能#xff0c;且独立存在则称为成为模块。下图为Prism体系中的关系结构图。在Prism体… 1.概要源码及PPT地址https://github.com/JusterZhu/wemail视频地址https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesourcecopywebModule具有特定功能且独立存在则称为成为模块。下图为Prism体系中的关系结构图。在Prism体系中Module的应用分为注册/发现模块加载模块初始化模块2.详细内容1注册/发现模块通过重写CreateModuleCatalog方法指定加载module的方式这里我个人比较推荐使用反射的方式去指定目录下读取当然还有其他方式这里就不一 一介绍了。首先我们将项目中的module编译生成到项目运行目录下的Apps文件夹下。这时需要在类库右键-点击属性。将DLL编译生成时拷贝到指定目录下详情见源码。/// summary
/// Interaction logic for App.xaml
/// /summary
public partial class App
{/// summary/// 应用程序启动时创建Shell/// /summary/// returns/returnsprotected override Window CreateShell(){return Container.ResolveMainWindow();}protected override void RegisterTypes(IContainerRegistry containerRegistry){//注册服务、依赖、View}/// summary/// 配置区域适配/// /summary/// param nameregionAdapterMappings/paramprotected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings){base.ConfigureRegionAdapterMappings(regionAdapterMappings);}protected override IModuleCatalog CreateModuleCatalog(){//new ConfigurationModuleCatalog()//指定模块加载方式为从文件夹中以反射发现并加载module(推荐用法)return new DirectoryModuleCatalog() { ModulePath .\Apps };}
}2加载模块加载模块的代码实现在DirectoryModuleCatalog内部实现大致如下//
// Summary:
// Represets a catalog created from a directory on disk.
//
// Remarks:
// The directory catalog will scan the contents of a directory, locating classes
// that implement Prism.Modularity.IModule and add them to the catalog based on
// contents in their associated Prism.Modularity.ModuleAttribute. Assemblies are
// loaded into a new application domain with ReflectionOnlyLoad. The application
// domain is destroyed once the assemblies have been discovered. The diretory catalog
// does not continue to monitor the directory after it has created the initialze
// catalog.
public class DirectoryModuleCatalog : ModuleCatalog
{private class InnerModuleInfoLoader : MarshalByRefObject{internal ModuleInfo[] GetModuleInfos(string path){DirectoryInfo directory new DirectoryInfo(path);ResolveEventHandler value (object sender, ResolveEventArgs args) OnReflectionOnlyResolve(args, directory);AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve value;ModuleInfo[] result GetNotAlreadyLoadedModuleInfos(IModuleType: AppDomain.CurrentDomain.GetAssemblies().First((Assembly asm) asm.FullName typeof(IModule).Assembly.FullName).GetType(typeof(IModule).FullName), directory: directory).ToArray();AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve - value;return result;}private static IEnumerableModuleInfo GetNotAlreadyLoadedModuleInfos(DirectoryInfo directory, Type IModuleType){ListAssembly list new ListAssembly();Assembly[] alreadyLoadedAssemblies (from p in AppDomain.CurrentDomain.GetAssemblies()where !p.IsDynamicselect p).ToArray();foreach (FileInfo item in (from file in directory.GetFiles(*.dll)where alreadyLoadedAssemblies.FirstOrDefault((Assembly assembly) string.Compare(Path.GetFileName(assembly.Location), file.Name, StringComparison.OrdinalIgnoreCase) 0) nullselect file).ToList()){try{list.Add(Assembly.LoadFrom(item.FullName));}catch (BadImageFormatException){}}return list.SelectMany((Assembly assembly) from t in assembly.GetExportedTypes().Where(new FuncType, bool(IModuleType.IsAssignableFrom))where t ! IModuleTypewhere !t.IsAbstractselect t into typeselect CreateModuleInfo(type));}private static Assembly OnReflectionOnlyResolve(ResolveEventArgs args, DirectoryInfo directory){Assembly assembly AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies().FirstOrDefault((Assembly asm) string.Equals(asm.FullName, args.Name, StringComparison.OrdinalIgnoreCase));if (assembly ! null){return assembly;}AssemblyName assemblyName new AssemblyName(args.Name);string text Path.Combine(directory.FullName, assemblyName.Name .dll);if (File.Exists(text)){return Assembly.ReflectionOnlyLoadFrom(text);}return Assembly.ReflectionOnlyLoad(args.Name);}internal void LoadAssemblies(IEnumerablestring assemblies){foreach (string assembly in assemblies){try{Assembly.ReflectionOnlyLoadFrom(assembly);}catch (FileNotFoundException){}}}private static ModuleInfo CreateModuleInfo(Type type){string name type.Name;Liststring list new Liststring();bool flag false;CustomAttributeData customAttributeData CustomAttributeData.GetCustomAttributes(type).FirstOrDefault((CustomAttributeData cad) cad.Constructor.DeclaringType!.FullName typeof(ModuleAttribute).FullName);if (customAttributeData ! null){foreach (CustomAttributeNamedArgument namedArgument in customAttributeData.NamedArguments){switch (namedArgument.MemberInfo.Name){case ModuleName:name (string)namedArgument.TypedValue.Value;break;case OnDemand:flag (bool)namedArgument.TypedValue.Value;break;case StartupLoaded:flag !(bool)namedArgument.TypedValue.Value;break;}}}foreach (CustomAttributeData item in from cad in CustomAttributeData.GetCustomAttributes(type)where cad.Constructor.DeclaringType!.FullName typeof(ModuleDependencyAttribute).FullNameselect cad){list.Add((string)item.ConstructorArguments[0].Value);}ModuleInfo obj new ModuleInfo(name, type.AssemblyQualifiedName){InitializationMode (flag ? InitializationMode.OnDemand : InitializationMode.WhenAvailable),Ref type.Assembly.EscapedCodeBase};obj.DependsOn.AddRange(list);return obj;}}//// Summary:// Directory containing modules to search for.public string ModulePath{get;set;}//// Summary:// Drives the main logic of building the child domain and searching for the assemblies.protected override void InnerLoad(){if (string.IsNullOrEmpty(ModulePath)){throw new InvalidOperationException(Prism.Properties.Resources.ModulePathCannotBeNullOrEmpty);}if (!Directory.Exists(ModulePath)){throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Prism.Properties.Resources.DirectoryNotFound, ModulePath));}AppDomain currentDomain AppDomain.CurrentDomain;try{Liststring list new Liststring();IEnumerablestring collection from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()where !(assembly is AssemblyBuilder) assembly.GetType().FullName ! System.Reflection.Emit.InternalAssemblyBuilder !string.IsNullOrEmpty(assembly.Location)select assembly.Location;list.AddRange(collection);Type typeFromHandle typeof(InnerModuleInfoLoader);if (typeFromHandle.Assembly ! null){InnerModuleInfoLoader innerModuleInfoLoader (InnerModuleInfoLoader)currentDomain.CreateInstanceFrom(typeFromHandle.Assembly.Location, typeFromHandle.FullName)!.Unwrap();base.Items.AddRange(innerModuleInfoLoader.GetModuleInfos(ModulePath));}}catch (Exception innerException){throw new Exception(There was an error loading assemblies., innerException);}}
}3初始化模块这些代码在使用Prism项目模板创建Module的时候就已经自动创建好了。[Module(ModuleName Contact)]
public class ContactModule : IModule
{//初始化public void OnInitialized(IContainerProvider containerProvider){//通过注册RegionManager添加ContactViewvar regionManager containerProvider.ResolveIRegionManager();//通过ContentRegion管理导航默认初始页面ContactViewvar contentRegion regionManager.Regions[ContentRegion];contentRegion.RequestNavigate(nameof(ContactView));}public void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterForNavigationContactView();}
}3.实战应用Shell - MainWindow实现Window.ResourcesStyle x:KeyModuleItemSytle TargetTypeListBoxItemSetter PropertyTemplateSetter.ValueControlTemplateGridTextBlock Text{Binding ModuleName}/TextBlock/Grid/ControlTemplate/Setter.Value/Setter/Style
/Window.Resources
GridGrid.ColumnDefinitionsColumnDefinition Width2*/ColumnDefinition Width8*//Grid.ColumnDefinitionsListBox ItemsSource{Binding Modules} SelectedItem{Binding ModuleInfo} ItemContainerStyle{StaticResource ModuleItemSytle} /ContentControl Grid.Column1 prism:RegionManager.RegionNameContentRegion/
/Grid
/WindowMainWindowViewModel中的实现public class MainWindowViewModel : BindableBase
{private string _title Prism Application;//Region管理对象private IRegionManager _regionManager;private IModuleCatalog _moduleCatalog;private ObservableCollectionIModuleInfo _modules;private DelegateCommand _loadModules;private IModuleInfo _moduleInfo;public IView View { get; set; }public string Title{get { return _title; }set { SetProperty(ref _title, value); }}public ObservableCollectionIModuleInfo Modules{get _modules ?? (_modules new ObservableCollectionIModuleInfo());}public DelegateCommand LoadModules { get _loadModules new DelegateCommand(InitModules); }public IModuleInfo ModuleInfo { get {return _moduleInfo; }set {_moduleInfo value;Navigate(value);}}public MainWindowViewModel(IRegionManager regionManager, IModuleCatalog moduleCatalog){_regionManager regionManager;_moduleCatalog moduleCatalog;}public void InitModules() {var dirModuleCatalog _moduleCatalog as DirectoryModuleCatalog;Modules.AddRange(dirModuleCatalog.Modules);}private void Navigate(IModuleInfo info) {_regionManager.RequestNavigate(ContentRegion, ${ info.ModuleName }View);}
}Module - Wemail.Contact实现ContactModule.cs 模块标记文件[Module(ModuleName Contact)]
public class ContactModule : IModule
{public void OnInitialized(IContainerProvider containerProvider){//通过注册RegionManager添加ContactViewvar regionManager containerProvider.ResolveIRegionManager();//通过ContentRegion管理导航默认初始页面ContactViewvar contentRegion regionManager.Regions[ContentRegion];contentRegion.RequestNavigate(nameof(ContactView));}public void RegisterTypes(IContainerRegistry containerRegistry){containerRegistry.RegisterForNavigationContactView();}
}ContactView实现GridGrid.ColumnDefinitionsColumnDefinition Width2*/ColumnDefinition Width8*//Grid.ColumnDefinitionsListBox x:NameLsbContact ItemsSource{Binding Contacts}/ContentControl HorizontalAlignmentCenter VerticalAlignmentCenter FontSize40 Grid.Column1 Content{Binding ElementNameLsbContact,PathSelectedItem}/ContentControl
/GridContactViewModel实现public class ContactViewModel : BindableBase
{private ObservableCollectionstring _contacts;private string _message;public string Message{get { return _message; }set { SetProperty(ref _message, value); }}public ObservableCollectionstring Contacts { get _contacts ?? (_contacts new ObservableCollectionstring()); }public ContactViewModel(){Message Wemail.Contact Prism Module;Contacts.Add(联系人张某);Contacts.Add(联系人王某);}
}