繁体   English   中英

不应用 generic.xaml 中的样式

[英]Styles from generic.xaml are not applied

我制作了一个类库程序集,在其中创建了自定义控件,并在 generic.xaml 文件中定义了默认样式。

只要有很多人发布它,这似乎是一个很常见的问题。 但是我找不到任何有用的答案。

  • generic.xaml 位于 Themes 文件夹中。<\/li>
  • 将generix.xaml 文件Build Action 设置为Page。<\/li>
  • ThemeInfo 在我的 AssemblyInfo.cs 中正确定义。<\/li><\/ul>

    在我的测试应用程序中,如果我手动将自定义控件程序集中的 generic.xaml 文件合并到应用程序 App.xaml 文件中,如下所示:

    那么自定义控件的主题是正确的,但如果我不手动合并 generic.xaml,这些控件将显示为默认的 Windows 主题。

    你能告诉我我忘记了什么和\/或做错了什么吗?

    附加信息:

    • 我的 ThemeInfo 程序集属性定义如下:

      (注意:对于 ThemeInfo 属性的任意参数组合,结果都是一样的)

    • Themes 文件夹中的 generic.xaml 文件旁边还有另外两个 .xaml 文件。

    • Themes 文件夹中有一个子文件夹,它本身包含另一个 .xaml 文件。<\/li><\/ul>"

您需要在自定义控件构造函数中使用以下行:

public class MyCustomControl : Control
{
    static MyCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
    }
}

然后,如果在themes文件夹中有generic.xaml文件,则使用以下示例样式:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border>
                    <Label>Testing...</Label>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在风格将自动应用,无需任何额外的合并。

要在我的自定义控件库中的Themes \\ Generic.xaml中应用默认样式,我决定从一个完善的开源控件库( MahApps )中寻找灵感。 该项目为您提供了一个如何构建和布局自定义控件库的非常好的示例。

简而言之,要在Themes \\ Generic.xaml中获取默认样式,您需要在自定义控件库的AssemblyInfo.cs文件中包含以下内容:

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

在我的例子中,我的自定义控件AssemblyInfo.cs看起来像:

using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Markup;

[assembly: AssemblyCopyright("...")]
[assembly: ComVisible(false)]

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0.0")]
[assembly: AssemblyTitleAttribute("...")]
[assembly: AssemblyDescriptionAttribute("")]
[assembly: AssemblyProductAttribute("...")]
[assembly: AssemblyCompany("...")]

不确定这是否适用于WPF,但它在Silverlight中适用于我:

构造函数:

public class MyCustomControl : Control
{
    static MyCustomControl()
    {
        this.Style = (Style)Application.Current.Resources["MyCustomControlStyle"];
    }
}

样式:

<Style x:Key="MyCustomControlStyle" TargetType="{local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{local:MyCustomControl}">
                <Border>
                    <Label>Testing...</Label>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

由于自定义控件程序集中的以下属性,我遇到了同样的问题:

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

UltimateResourceFallbackLocation.Satellite更改为UltimateResourceFallbackLocation.MainAssembly或删除第二个参数完全解决了我的问题(如果您不需要定义程序集的中性语言,也可以删除该属性)。

在我的情况下,我忘记从<Style>标记中删除x:Key

错误的:

<Style TargetType="controls:IpTextBox" x:Key="MyControlStyle">

正确的:

<Style TargetType="controls:IpTextBox">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM