这是可能的,所以我为任何偶然发现我原来的问题的人发布了解决方案。
修复方法是将所需样式合并到 MyCompany.Controls.dll 程序集中每个自定义控件的 ResourceDictionary 中。虽然我在 XAML 中没有成功执行此操作,但只需将以下调用添加到每个自定义控件的构造函数中:
this.MergeInResources("/MyCompany.Styles;component/default.xaml");
神奇的扩展方法如下:
/// <summary>
/// Loads the resources pointed to by the (relative) path
/// <paramref name="resourcePath"/> into the Merged Dictionaries
/// of <paramref name="control"/>.
/// </summary>
internal static void MergeInResources(this FrameworkElement control,
string resourcePath)
{
if (String.IsNullOrWhiteSpace(resourcePath))
throw new ArgumentNullException("resourcePath");
Uri uri = new Uri(resourcePath, UriKind.Relative);
ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(uri);
control.Resources.MergedDictionaries.Add(dictionary);
}
错误处理和 ResourceDictionary 缓存 + 共享留给读者。