[Export]
attribute inheritance supportIUnityContainer
occurrences as a class constructor or public property. If other parameters are passed to the constructor from the container (constructor injection), then we leave them, and we mark the constructor with the [ImportingConstructor]
attribute.//Prism 2.2 <br> public class SampleClass<br>{<br> private readonly IRegionManager regionManager;<br> private readonly IUnityContainer container;<br><br> public SampleClass(IUnityContainer container, IRegionManager regionManager)<br> {<br> this .container = container;<br> this .regionManager = regionManager;<br> }<br>}<br><br> //Prism 4 <br>[Export( typeof (SampleClass))]<br> public class SampleClass<br>{<br> private readonly IRegionManager regionManager;<br><br> [ImportingConstructor]<br> public SampleClass(IRegionManager regionManager)<br> {<br> this .regionManager = regionManager;<br> }<br>}<br> <br> * This source code was highlighted with Source Code Highlighter .
[Export]
attribute (you can use [InheritedExport]
for interfaces), and the class registration code in the container should be removed at the same time.[ModuleExport(typeof(<ModuleType>))]
attribute (the definition of this attribute is in the Microsoft.Practices.Prism.MefExtensions.dll assembly).//Prism 2.2 <br> public class SampleModule : IModule<br>{<br> public void Initialize()<br> {<br> Register();<br> }<br><br> public void Register()<br> {<br> container.RegisterType<ISampleViewModel, SampleViewModel>();<br> }<br>}<br><br> //Prism 4 <br>[ModuleExport( typeof (SampleClass))]<br> public class SampleModule : IModule<br>{<br> public void Initialize()<br> {<br> ...<br> }<br>}<br><br>[InheritedExport]<br> public interface ISampleViewModel<br>{<br> ...<br>} <br><br> * This source code was highlighted with Source Code Highlighter .
Bootstarapper
class should be re-inherited from MefBootstrapper
CreateShell()
method and add the InitializeShell()
methodGetModuleCatalog()
method should be renamed to CreateModuleCatalog()
ConfigureAggregateCatalog()
method should be redefined to include all the assemblies that are available from the current assembly.//Prism 4 <br> using System.ComponentModel.Composition;<br> using System.ComponentModel.Composition.Hosting;<br><br> using Microsoft.Practices.Prism.MefExtensions;<br> using Microsoft.Practices.Prism.Regions;<br> using Modularity = Microsoft.Practices.Prism.Modularity;<br><br> namespace Sample.Shell<br>{<br> public class Bootstrapper : MefBootstrapper<br> {<br> protected override DependencyObject CreateShell()<br> {<br> return Container.GetExportedValue<ShellView>();<br> }<br><br> protected override void InitializeShell()<br> {<br> base .InitializeShell();<br> Application.Current.RootVisual = (ShellView) this .Shell;<br> }<br><br> protected override Modularity.IModuleCatalog CreateModuleCatalog()<br> {<br> return Modularity.ModuleCatalog.CreateFromXaml( new Uri ( "/Sample.Shell;component/ModulesCatalog.xaml" , UriKind.Relative));<br> }<br><br> protected override void ConfigureAggregateCatalog()<br> {<br> base .ConfigureAggregateCatalog();<br> AggregateCatalog.Catalogs.Add( new AssemblyCatalog( this .GetType(). Assembly ));<br> }<br> }<br>}<br> <br> * This source code was highlighted with Source Code Highlighter .
CreationPolicy
parameter is not specified during import or export, the class will be used as Shared (one instance for all calls)ContainerControlledLifetimeManager()
, then in this case such class must be marked as Shared
. Otherwise, this class may be instantiated several times with the [Import(RequiredCreationPolicy=CreationPolicy.NonShared)]
attribute specified [Import(RequiredCreationPolicy=CreationPolicy.NonShared)]
//Prism 2.2 <br> private void RegisterTypes()<br>{<br> SampleType sampleType = new SampleType(...);<br> Container.RegisterInstance<ISampleType>(sampleType);<br><br> Container.RegisterType<ISingletonSampleType, SingletonSampleType>( new ContainerControlledLifetimeManager());<br>}<br><br> //Prism 4 <br>[Export]<br>[PartCreationPolicy(CreationPolicy.Shared)]<br> public class SampleType : ISampleType<br>{<br>...<br>}<br><br>[Export]<br>[PartCreationPolicy(CreationPolicy.Shared)]<br> public class SingletonSampleType : ISingletonSampleType<br>{<br>...<br>}<br> <br> * This source code was highlighted with Source Code Highlighter .
//Prism 2.2 <br> private void Foo()<br>{<br> List <ISampleType> samples = new List <ISampleType>();<br> for ( int i = 0; i == 10; i++)<br> {<br> ISampleType sampleTypeInstance = Container.ResolveInstance<ISampleType>();<br> samples.Add(sampleTypeInstance);<br> }<br>}<br><br> //Prism 4 <br>[Import]<br> public ExportFactory<ISampleType> SampleTypeFactory<br>{<br> get ;<br> set ;<br>}<br><br> private void Foo()<br>{<br> List <ISampleType> samples = new List <ISampleType>();<br> for ( int i = 0; i == 10; i++)<br> {<br> ISampleType sampleTypeInstance = SampleTypeFactory.CreatExport().Value;<br> samples.Add(sampleTypeInstance);<br> }<br>}<br> <br> * This source code was highlighted with Source Code Highlighter .
NotificationObject
class. This class implements the INotifyPropertyChanged
interface and implements another version of the RaisePropertyChanged()
method, which accepts not the property's string name, but the property itself. It is highly desirable to abandon the old implementation of RaisePropertyChanged()
and start using the new one.//Prism 2.2 <br> public class SampleViewModel : ISampleViewModel<br>{<br> private SamplePropertyType sampleProperty;<br> public SamplePropertyType SampleProperty<br> {<br> get <br> {<br> return sampleProperty;<br> }<br> set <br> {<br> sampleProperty = value ;<br> this .RaisePropertyChanged( "SampleProperty" );<br> }<br> }<br>}<br><br> //Prism 4 <br> public class SampleViewModel : NotificationObject, ISampleViewModel<br>{<br> private SamplePropertyType sampleProperty;<br> public SamplePropertyType SampleProperty<br> {<br> get <br> {<br> return sampleProperty;<br> }<br> set <br> {<br> sampleProperty = value ;<br> this .RaisePropertyChanged(() => SampleProperty);<br> }<br> }<br>}<br> <br> * This source code was highlighted with Source Code Highlighter .
Message: Unabled Error in Silverlight Application Code: 4004 Category: ManagedRuntimeError Message: Microsoft.Practices.Prism.Modularity.ModuleTypeLoadingException: Failed to load type for module <ModuleType>. Error was the composition remains unchanged. 13 root causes. The root causes are provided below. Review the CompositionException.Errors property for more detailed information.
1) Microsoft. Prism.MefExtensions.Modularity.MefModuleManager '.
...
CopyLocal=False
property to all duplicate assemblies or use the plugin for VisualStudio2010 .<!--ModulesCatalog.xaml, Prism 2.2--> <br> < Modularity:ModuleCatalog xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br> xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" <br> xmlns:Modularity ="clr-namespace:Microsoft.Practices.Composite.Modularity; assembly=Microsoft.Practices.Composite" > <br> < Modularity:ModuleInfo <br> Ref ="SampleModule.xap" <br> ModuleName ="SampleModule" <br> ModuleType ="SampleModule.SampleModule, SampleModule, Version=1.0.0.0" /> <br> </ Modularity:ModuleCatalog > <br><br> <!--ModulesCatalog.xaml, Prism 4--> <br> < Modularity:ModuleCatalog xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br> xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" <br> xmlns:Modularity ="clr-namespace:Microsoft.Practices.Prism.Modularity; assembly=Microsoft.Practices.Prism" > <br> < Modularity:ModuleInfo <br> Ref ="SampleModule.xap" <br> ModuleName ="SampleModule" <br> ModuleType ="SampleModule.SampleModule, SampleModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null " /> <br> </ Modularity:ModuleCatalog > <br> <br> * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/106726/
All Articles