您可以使用 MEF,从每个视图模型中导出设置视图,并将它们作为视图列表导入到堆栈面板或主设置视图中的某个视图中。
使用 MEF 的一个很好的信息来源是:http ://mef.codeplex.com/wikipage?title=Guide
这是我打算早点起床的示例程序:
使用系统;使用 System.Collections.Generic;使用 System.ComponentModel.Composition;使用 System.ComponentModel.Composition.Hosting;使用 System.Reflection;
namespace zTestConsole
{
public interface ISimple
{
string Message { get; }
}
[Export("SimpleHello",typeof(ISimple))]
public class SimpleHello : ISimple
{
[Export("Message")]
public string Message
{
get { return "Silverlight rocks!"; }
}
}
[Export("SimpleBello",typeof(ISimple))]
public class SimpleBello : ISimple
{
[Export("Message")]
public string Message
{
get { return "C# rocks!"; }
}
}
public class SimpleMultiCat
{
[ImportMany("Message")]
public IEnumerable<string> Messages { get; set; }
}
public class SimpleCat
{
[Import("SimpleHello")]
public ISimple simple { get; set; }
}
class Program
{
private static CompositionContainer container;
static void Main(string[] args)
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
SimpleMultiCat cats = new SimpleMultiCat();
SimpleCat cat = new SimpleCat();
Program.container = new CompositionContainer(catalog);
try
{
Program.container.ComposeParts(cats);
foreach (string message in cats.Messages)
{
Console.WriteLine(message);
}
}
catch (CompositionException ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine();
try
{
container.ComposeParts(cat);
Console.WriteLine(cat.simple.Message);
}
catch (CompositionException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}