我怎样才能制作“私人”?资源字典?
我正在构建一个复杂的 ResourceDictionary,它将公开我想要与包含该 ResourceDictionary 的项目共享的 ControlTemplates。但是,此 ResourceDictionary 包含一系列我不希望在项目中使用的支持样式和模板。我该怎么做?
例如:
<ResourceDictionary>
<!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
<ResourceDictionary x:Key="PrivateDictionary">
<Thickness x:Key="BaseValueMarginAdjustment">2,0,0,0</Thickness>
<!--Base Styles -->
<Style x:Key="BaseElement" TargetType="FrameworkElement">...</Style>
<Style x:Key="GridStyle" TargetType="Grid" BasedOn="{StaticResource BaseElement}">...</Style>
<Style TargetType="Selector" x:Key="SelectorStyle" BasedOn="{StaticResource BaseElement}">...</Style>
...
</ResourceDictionary>
<!--Public CONTROL TEMPLATES -->
<ControlTemplate TargetType="{x:Type local:OmniBox}" x:Key="OBListBoxTemplate">
<Grid x:Name="PART_Grid" Style="{StaticResource GridStyle}">
<ListBox x:Name="PART_Value" Style="{StaticResource SelectorStyle}" />
...
</ControlTemplate>
<ControlTemplate ...>
...
</ResourceDictionary>
注意上面的方法不起作用。特别是,上面的代码可以编译,但会出现运行时错误,因为我想要公开可见的 ControlTemplates 找不到上述私有样式,例如“GridStyle”。
尝试了以下方法也没有成功:
<ResourceDictionary>
<!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>...</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<!--CONTROL TEMPLATES -->
<ControlTemplate TargetType="{x:Type local:OmniBox}" x:Key="OBTextBoxTemplate">
...
Possible Duplicate:
How to make a Style that only exists within the context of a ResourceDictionary
I'm building a complex ResourceDictionary which will expose ControlTemplates I want to share with projects that include that ResourceDictionary. However, this ResourceDictionary contains a series of supporting styles and templates which I don't want available to the projects. How can I do this?
For example:
<ResourceDictionary>
<!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
<ResourceDictionary x:Key="PrivateDictionary">
<Thickness x:Key="BaseValueMarginAdjustment">2,0,0,0</Thickness>
<!--Base Styles -->
<Style x:Key="BaseElement" TargetType="FrameworkElement">...</Style>
<Style x:Key="GridStyle" TargetType="Grid" BasedOn="{StaticResource BaseElement}">...</Style>
<Style TargetType="Selector" x:Key="SelectorStyle" BasedOn="{StaticResource BaseElement}">...</Style>
...
</ResourceDictionary>
<!--Public CONTROL TEMPLATES -->
<ControlTemplate TargetType="{x:Type local:OmniBox}" x:Key="OBListBoxTemplate">
<Grid x:Name="PART_Grid" Style="{StaticResource GridStyle}">
<ListBox x:Name="PART_Value" Style="{StaticResource SelectorStyle}" />
...
</ControlTemplate>
<ControlTemplate ...>
...
</ResourceDictionary>
Note that the above does not work. In particular, the above compiles, but has a runtime error because the ControlTemplates I want publically visible can't find the above private styles like "GridStyle".
Tried the following without success as well:
<ResourceDictionary>
<!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>...</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<!--CONTROL TEMPLATES -->
<ControlTemplate TargetType="{x:Type local:OmniBox}" x:Key="OBTextBoxTemplate">
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不能将这些样式资源放在单独的资源字典中?因此,在您不希望这些资源可见的项目中,只需不要将它们合并到那里即可。
假设您在 App.xaml 中定义了资源,如下所示 -
假设您的样式资源存在于 Dictionary2.xaml 中,只需从中省略第二个字典即可。
编辑:
您可以在 ResourceDictionary2 中添加 ResourceDictionary1 的引用。无论您希望使用资源,例如任何 UserControl,您都可以随时添加 ResourceDictionary1 的引用,以防您不需要全局引用。
Why can't you place these Resources for Styles in separate Resource Dictionary? So, across project where you don't want these Resources to be visible, simply don't merge them there.
Suppose you have Resource define in your App.xaml like this -
Suppose your Style Resources exist in Dictionary2.xaml, simply omit the second dictionary from it.
Edit:
You can add the reference of ResourceDictionary1 in your ResourceDictionary2. And wherever you want the reosurces to be used say any UserControl you can always add reference of ResourceDictionary1 there in case you dont want the global reference.