wpf Prism:如何使用 Unity Bootstrapper 在 Shell 区域加载默认视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34569626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prism: How to load Default View in Shell Region with Unity Bootstrapper
提问by Steve.NayLinAung
When I look PrismNavigation QuickStart demo, it uses Mef Bootstrapperand use IPartImportsSatisfiedNotificationinterface in Shell.xaml.csfile to load default view into the Shell Region as following.
当我查看PrismNavigation QuickStart 演示时,它使用Mef Bootstrapper并使用文件中的IPartImportsSatisfiedNotification接口Shell.xaml.cs将默认视图加载到 Shell 区域中,如下所示。
[Export]
public partial class Shell : UserControl, IPartImportsSatisfiedNotification
{
...
public void OnImportsSatisfied()
{
this.ModuleManager.LoadModuleCompleted +=
(s, e) =>
{
if (e.ModuleInfo.ModuleName == EmailModuleName)
{
this.RegionManager.RequestNavigate(
RegionNames.MainContentRegion,
InboxViewUri);
}
};
}
}
In my project, I use Unity Bootstrapperand try to reference this demo for loading default view. As expected, it totally does not work.
在我的项目中,我使用Unity Bootstrapper并尝试参考此演示来加载默认视图。正如预期的那样,它完全不起作用。
Please, share me suggestions and some recommendations on "How to inject Default View into Shell Region with Unity Bootstrapper".
请与我分享关于“如何使用 Unity Bootstrapper 将默认视图注入 Shell 区域”的建议和一些建议。
回答by R. Richards
Assuming you have a CreateModuleCatalog override in your bootstrapper, you use it to add a module to the catalog.
假设您的引导程序中有一个 CreateModuleCatalog 覆盖,您可以使用它向目录添加一个模块。
catalog.AddModule(typeof(YourModule));
In the YourModule Initiaize override, register the view you want displayed like below.
在 YourModule Initiaize 覆盖中,注册您想要显示的视图,如下所示。
Using view discovery:
使用视图发现:
RegionManager.RegisterViewWithRegion("YourRegion", typeof(YourView));
OR
或者
Using view injection (if you want a little more control, or need a scoped region, etc.):
使用视图注入(如果你想要更多的控制,或者需要一个作用域,等等):
IRegion region = _region_manager.Regions["YourRegion"];
var view = _container.Resolve<YourView>();
region.Add(view, typeof(YourView).FullName);
region.Activate(view);
This injection method needs you have to a reference to the region manager, and you have registered the view with the Unity container, and you have a reference to the container
这种注入方式需要你有一个region manager的引用,并且你已经把view注册到了Unity容器中,并且你有一个对容器的引用
As long as the YourRegion region is in your Shell xaml, and is visible at runtime, the YourView will show in it.
只要 YourRegion 区域在您的 Shell xaml 中,并且在运行时可见,YourView 就会显示在其中。
The Hello World quick start shows this too, and uses the Unity container.
Hello World 快速入门也显示了这一点,并使用了 Unity 容器。
https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/HelloWorld
https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/HelloWorld
回答by Brian Lagunas
Just add the view to the region in your Module.Initialize in which the view is coming from.
只需将视图添加到您的 Module.Initialize 中视图来自的区域。
回答by zyqcome z
I have encountered this problem If you use prism.Unity, wpf
我遇到过这个问题如果你用prism.Unity,wpf
you can set
你可以设置
protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
var factory = base.ConfigureDefaultRegionBehaviors();
factory.AddIfMissing("AutoPopulateExportedViewsBehavior",typeof(AutoPopulateExportedViewsBehavior));
return factory;
}
after add new class for "AutoPopulateExportedViewsBehavior"
为“AutoPopulateExportedViewsBehavior”添加新类后
public class AutoPopulateExportedViewsBehavior : RegionBehavior
{
public static string Key {
get;
} = nameof( AutoPopulateExportedViewsBehavior );
protected override void OnAttach()
{
this.Region.ActiveViews.CollectionChanged += this.ActiveViews_CollectionChanged;
}
private void ActiveViews_CollectionChanged( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e )
{
switch ( e.Action )
{
case NotifyCollectionChangedAction.Add:
Action<IRegionManagerAware> setRegionManager = x => x.RegionManager = this.Region.RegionManager;
MvvmHelpers.ViewAndViewModelAction( e.NewItems[0], setRegionManager );
break;
case NotifyCollectionChangedAction.Remove:
Action<IRegionManagerAware> resetRegionManager = x => x.RegionManager = null;
MvvmHelpers.ViewAndViewModelAction( e.OldItems[0], resetRegionManager );
break;
}
}
}
and you need a interface to find the "RegionManager"
并且您需要一个界面来找到“RegionManager”
public interface IRegionManagerAware
{
IRegionManager RegionManager {
get; set;
}
}
last,Tell the program that you need a default view,
最后,告诉程序你需要一个默认视图,
public class ModulesModule : IModule
{
IRegionManager _regionManager;
IUnityContainer _container;
public ModulesManagementModule( RegionManager regionManager, IUnityContainer container )
{
_regionManager = regionManager;
_container = container;
}
public void Initialize()
{
_container.RegisterTypeForNavigation<ViewA>();
_regionManager.RequestNavigate("ViewA", "ViewA" );
}
}
Ideas and solutions are from : enter link description here
想法和解决方案来自: 在此处输入链接描述
Through this example, you can get a default view of the Navigate
通过这个例子,你可以得到一个默认的 Navigate 视图
回答by kipzes
You've to load/register your modules. You can do this in many ways.
您必须加载/注册您的模块。您可以通过多种方式做到这一点。
Register/Load in Code
注册/加载代码
In the bootstrapper.cs:
在bootstrapper.cs:
protected override void ConfigureModuleCatalog()
{
Type ModuleAType = typeof(ModuleAModule);
ModuleCatalog.AddModule(new ModuleInfo()
{
ModuleName = moduleAType.Name,
ModuleType = moduleAType.AssemblyQualifiedName,
InitializationMode = InitializationMode.WhenAvailable
});
}
Register/Load from Directory
从目录注册/加载
In the bootstrapper.cs:
在bootstrapper.cs:
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
}
Create the Modulesdirectory in the bin\Debug directory and copy/paste the ModuleA.dllfile to this directory.
Modules在 bin\Debug 目录中创建目录并将ModuleA.dll文件复制/粘贴到此目录。
In the ModuleAModulle.csyou can define the module name and initialization method:
在中ModuleAModulle.cs可以定义模块名称和初始化方法:
[Module(ModuleName="ModuleA", OnDemand=true)]
public class ModuleAModule : IModule
{
public void Initialize()
{
throw new NotImplementedException();
}
}
Register/Load from XAML File
从 XAML 文件注册/加载
Add a new Resource Dictionary to your shell project and set the build action to Resource. (In this example it's called XamlCatalog.xaml)
将新的资源字典添加到您的 shell 项目并将构建操作设置为资源。(在这个例子中它被称为XamlCatalog.xaml)
<Modularity:ModuleCatalog
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<Modularity:ModuleInfo Ref="file://ModuleA.dll" ModuleName="ModuleA" ModuleType="ModuleA.ModuleAModule, ModuleA, Version=1.0.0.0" InitializationMode="WhenAvailable" />
</Modularity:ModuleCatalog>
In the bootstrapper.cs:
在bootstrapper.cs:
protected override IModuleCatalog CreateModuleCatalog()
{
return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
new Uri("/ProjectNameHere;component/XamlCatalog.xaml", UriKind.Relative));
}
Don't forget to copy/paste the ModuleA.dllfile to the root of the project, because you're referencing to it in the XamlCatalog.xamlfile.
不要忘记将ModuleA.dll文件复制/粘贴到项目的根目录,因为您在XamlCatalog.xaml文件中引用了它。
Register/Load from App.config File
从 App.config 文件注册/加载
In the bootstrapper.cs:
在bootstrapper.cs:
protected override IModuleCatalog CreateModuleCatalog()
{
return new ConfigurationModuleCatalog();
}
In the 'App.config':
在“App.config”中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<modules>
<module assemblyFile="Modules/ProjectNameHere.ModuleA.dll" moduleType="ProjectNameHere.ModuleA.ModuleAModule, ProjectNameHere.ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleA" startupLoaded="true" />
<module assemblyFile="Modules/ProjectNameHere.ModuleB.dll" moduleType="ProjectNameHere.ModuleB.ModuleBModule, ProjectNameHere.ModuleB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleB" startupLoaded="true" />
<module assemblyFile="Modules/ProjectNameHere.ModuleC.dll" moduleType="ProjectNameHere.ModuleC.ModuleCModule, ProjectNameHere.ModuleC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleC" startupLoaded="true" />
</modules>
</configuration>
I've got this information from the Prims Introduction course on Pluralsight.
我从Pluralsight 的 Prims 介绍课程中获得了这些信息。

