0

面向 .NET Framework 4.0。

我有一个 UserControl ( usercontrol.xaml),其中包含一些我希望可以从应用程序的资源 ( app.xaml) 中访问的资源。(我的 UserControl 被实例化了MainWindow.xaml。)

到目前为止,我已经在 app.xaml 中尝试过:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="usercontrol.xaml"/>
</ResourceDictionary.MergedDictionaries>

但 Visual Studio 对此抱怨: 查找资源字典“usercontrol.xaml”时出错。

问:如何将 UserControl 的资源添加到应用程序中,以便在运行时执行此操作:

Style style = FindResource("SomeStyleDefinedInUserControl") as Style;
4

1 回答 1

2

如果您想共享样式,请将它们放在单独的资源字典中。

然后,像您一样将资源字典添加到 App 的合并字典中:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="usercontrol_styles.xaml"/>
</ResourceDictionary.MergedDictionaries>

现在可以从应用程序中的任何位置访问这些样式。

于 2012-05-09T08:08:46.830 回答