Тези дни направих 2 проекта, които правят това, което искате. това, което направих, е да създам основен проект, който не прави нищо повече от събиране на модулите чрез MEF и управление на избора на модула. вие също се нуждаете от компонентен проект, където настройвате интерфейсите и експортните атрибути.
ето някои примерни кодови части:
MainProject app.xaml.cs
public partial class App : Application
{
[ImportMany]
private IEnumerable<Lazy<IComponent, IComponentMetadata>> _components;
private CompositionContainer _mefcontainer;
protected override void OnStartup(StartupEventArgs e)
{
ShutdownMode = ShutdownMode.OnExplicitShutdown;
//i do login stuff here
//i use task.factory here and dynamic splashscreen here
this.MefContainer.ComposeParts(rahmen, this);
foreach (var component in _components)
{
//check metadata and fill modules collection
}
//add modules collection to mainwindowviewmodel
ShutdownMode = ShutdownMode.OnMainWindowClose;
this.MainWindow.Show();
}
}
Component.dll
public interface IComponent//Marker interface
{
bool HasChanges { get; }
}
public interface IComponentMetadata
{
string Displayname { get; }
int SortIndex { get; }
string ImagePath { get; }
string IconPath { get; }
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ComponentExportAttribute : ExportAttribute, IComponentMetadata
{
private const string DEFAULTICON = "pack://application:,,,/MyComponent;Component/Images/Default.png";
public ComponentExportAttribute(string displayname, int sortindex): base(typeof(IComponent))
{
this.Displayname = displayname;
this.SortIndex = sortindex;
this.ImagePath = DEFAULTICON;
this.IconPath = DEFAULTICON;
}
public ComponentExportAttribute(string displayname, int sortindex, string imagepath, string iconpath): base(typeof(IComponent))
{
this.Displayname = displayname;
this.SortIndex = sortindex;
this.ImagePath = String.IsNullOrWhiteSpace(imagepath) ? DEFAULTICON : imagepath;
this.IconPath = String.IsNullOrWhiteSpace(iconpath) ? DEFAULTICON : iconpath;
}
#region Implementation of IComponentMetadata
public string Displayname { get; private set; }
public int SortIndex { get; private set; }
public string ImagePath { get; private set; }
public string IconPath { get; private set; }
#endregion
}
Пример за модул
[ComponentExport("Test1", 150
, "pack://application:,,,/TestProject;Component/Test/Logo1.png"
, "pack://application:,,,/TestProject;Component/Test/Icon1.png")]
public partial class Test1MainWindow : UserControl, IComponent
{
[ImportingConstructor]//if you want to do DI
public Test1MainWindow ()
{
InitializeComponent();
this.DataContext = this;
}
#region Implementation of IComponent
public bool HasChanges
{
get { return false; }
}
#endregion
}
или експортиране на viewmodel --> (ако направите това, трябва да експортирате шаблон за данни в основното приложение, мога да покажа това, ако искате)
[ComponentExport("Test2", 500
, "pack://application:,,,/TestProject;Component/Test/Logo2.png"
, "pack://application:,,,/TestProject;Component/Test/Icon2.png")]
public class Test2: INPCBase, IComponent
{
[ImportingConstructor]
public Test2()
{
}
#region Implementation of IKabuComponent
public bool HasChanges
{
get { return false; }
}
#endregion
}
person
blindmeis
schedule
21.02.2012