是否可以找到或定义资源 from/in 图书馆?
is it possible to find or define resources from/in a library?
我有一个 WPF 应用程序 here,它位于 MVVM 模式中,使用 MVVMLight 作为框架和一个 MEF 插件,带有一个名为 VCNEditor.
的用户控件
主应用程序使用一个名为 DialogViewModel 的 DialogBehavior 类型的 Attached Behaviour 来显示对话框。 DialogBehavior 负责将视图绑定到其预先创建的 DialogViewModel : IDialogViewModel
(首先是视图模型),方法是:
var resource = Application.Current.TryFindResource(viewModel.GetType());
该插件是一个带有用户控件的库。 UserControl 是从“Extensions”目录动态延迟加载的,只有在存在时才可见。
到目前为止一切正常。我目前面临的问题是:我试图以与在主应用程序中相同的方式在我的插件中显示对话框。
含义:我定义了一个属性类型为
的名为“Dialogs”
ObservableCollection<IDialogViewModel>
在我的 UserControlViewModel : IDialogViewModel
和数据绑定中 属性 到我的 附加行为。
当我现在将新的 DialogViewModel 添加到该集合时
Application.Current.TryFindResource(viewModel.GetType())
无法找到任何资源,因为 Application.Current 正在返回主应用程序,而库在主应用程序中没有像 app.xaml 中那样的任何资源字典。
我无法将插件对话框的资源添加到主应用程序,因为这会破坏我松散耦合的插件架构。
我只是一个临时开发者,如果有人能指出我正确的方向就好了。
当前的解决方法是,我在每个视图模型构造器中“手动”创建视图。不过我觉得这个确实很好看
感谢您的支持。
如果我没理解错的话,您可以轻松地将您的插件样式和资源添加到 Application.Current.Resources
代码中。
解决这个问题的一种方法是为您的插件创建一个合并的字典架构。在你的 MEF 插件 Interface 上实现 HasResource
bool
属性, 和 ResourceDictionary/ResourceDictionaries
属性.
插件加载时,只需检查这些属性,如果有资源,只需通过
将它们添加到主机应用程序
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
其他资源
Application.Resources Property
Gets or sets a collection of application-scope resources, such as
styles and brushes.
ResourceDictionary.MergedDictionaries Property
Gets a collection of the ResourceDictionary dictionaries that
constitute the various resource dictionaries in the merged
dictionaries.
Provides a hash table / dictionary implementation that contains WPF
resources used by components and other elements of a WPF application.
我有一个 WPF 应用程序 here,它位于 MVVM 模式中,使用 MVVMLight 作为框架和一个 MEF 插件,带有一个名为 VCNEditor.
的用户控件主应用程序使用一个名为 DialogViewModel 的 DialogBehavior 类型的 Attached Behaviour 来显示对话框。 DialogBehavior 负责将视图绑定到其预先创建的 DialogViewModel : IDialogViewModel
(首先是视图模型),方法是:
var resource = Application.Current.TryFindResource(viewModel.GetType());
该插件是一个带有用户控件的库。 UserControl 是从“Extensions”目录动态延迟加载的,只有在存在时才可见。
到目前为止一切正常。我目前面临的问题是:我试图以与在主应用程序中相同的方式在我的插件中显示对话框。
含义:我定义了一个属性类型为
的名为“Dialogs”ObservableCollection<IDialogViewModel>
在我的 UserControlViewModel : IDialogViewModel
和数据绑定中 属性 到我的 附加行为。
当我现在将新的 DialogViewModel 添加到该集合时
Application.Current.TryFindResource(viewModel.GetType())
无法找到任何资源,因为 Application.Current 正在返回主应用程序,而库在主应用程序中没有像 app.xaml 中那样的任何资源字典。
我无法将插件对话框的资源添加到主应用程序,因为这会破坏我松散耦合的插件架构。
我只是一个临时开发者,如果有人能指出我正确的方向就好了。 当前的解决方法是,我在每个视图模型构造器中“手动”创建视图。不过我觉得这个确实很好看
感谢您的支持。
如果我没理解错的话,您可以轻松地将您的插件样式和资源添加到 Application.Current.Resources
代码中。
解决这个问题的一种方法是为您的插件创建一个合并的字典架构。在你的 MEF 插件 Interface 上实现 HasResource
bool
属性, 和 ResourceDictionary/ResourceDictionaries
属性.
插件加载时,只需检查这些属性,如果有资源,只需通过
将它们添加到主机应用程序Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
其他资源
Application.Resources Property
Gets or sets a collection of application-scope resources, such as styles and brushes.
ResourceDictionary.MergedDictionaries Property
Gets a collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.
Provides a hash table / dictionary implementation that contains WPF resources used by components and other elements of a WPF application.