我怎样才能制作“私人”?资源字典?

发布于 2024-12-09 14:09:22 字数 2129 浏览 1 评论 0原文

可能的重复:
如何制作仅存在于 ResourceDictionary 上下文中的样式

我正在构建一个复杂的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-16 14:09:22

为什么不能将这些样式资源放在单独的资源字典中?因此,在您不希望这些资源可见的项目中,只需不要将它们合并到那里即可。

假设您在 App.xaml 中定义了资源,如下所示 -

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

假设您的样式资源存在于 Dictionary2.xaml 中,只需从中省略第二个字典即可。

编辑:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

您可以在 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 -

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Suppose your Style Resources exist in Dictionary2.xaml, simply omit the second dictionary from it.

Edit:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文