我发现当插件被激活时,它的 Application.Current 为空。之前我没有向每个窗口添加资源,但是每个插件库都包含带有资源的 App.xaml 文件。因此,项目编译成功,VS WPF Designers 运行良好,当我使用 MEF(在一个应用程序域中)时它运行良好。但是,正如我所说,当我尝试 MAF 时,插件没有 Application.Current 实例,所以它根本没有资源。
现在,为了让每个 AddIn 都使用宿主应用程序的样式,我创建了合约:
public interface IThemeContract : IContract
{
/// <summary>
/// Theme name
/// </summary>
string Name { get; }
/// <summary>
/// Theme description
/// </summary>
string Description { get; }
/// <summary>
/// Array of expected resources dictionaries
/// </summary>
IListContract<string> ResourceDictionaries { get; }
}
宿主应用程序序列化其所有资源;一块HostAdapter:
System.AddIn.Contract.IListContract<string> IThemeContract.ResourceDictionaries
{
get
{
var list = new List<string>();
foreach (var s in _view.ResourceDictionaries)
{
list.Add(XamlWriter.Save(s));
}
return CollectionAdapters.ToIListContract<string>(list);
}
}
并将其发送到插件;然后 addins 反序列化它;一块 AddInAdapter:
public override System.Windows.ResourceDictionary[] ResourceDictionaries
{
get
{
var strings = CollectionAdapters.ToIList<string>(_contract.ResourceDictionaries);
var rds = new List<System.Windows.ResourceDictionary>();
foreach (var s in strings)
{
var output = XamlReader.Parse(s);
if ((output as System.Windows.ResourceDictionary) != null)
rds.Add(output as System.Windows.ResourceDictionary);
}
return rds.ToArray();
}
}
然后应用程序看起来像一个单元。