简体   繁体   English

不应用 generic.xaml 中的样式

[英]Styles from generic.xaml are not applied

I made a class library assembly in which I created custom controls, and I defined the default styles in the generic.xaml file.我制作了一个类库程序集,在其中创建了自定义控件,并在 generic.xaml 文件中定义了默认样式。

It seems this is a quite common problem, as long as many people are posting about it.只要有很多人发布它,这似乎是一个很常见的问题。 However I couldn't find any useful answer for my case.但是我找不到任何有用的答案。

  • the generic.xaml is in the Themes folder. generic.xaml 位于 Themes 文件夹中。<\/li>
  • the generix.xaml file Build Action is set to Page.将generix.xaml 文件Build Action 设置为Page。<\/li>
  • the ThemeInfo is properly defined in my AssemblyInfo.cs. ThemeInfo 在我的 AssemblyInfo.cs 中正确定义。<\/li><\/ul>

    In my test application, if I manually merge the generic.xaml file from my custom controls assembly into the application App.xaml file like this:在我的测试应用程序中,如果我手动将自定义控件程序集中的 generic.xaml 文件合并到应用程序 App.xaml 文件中,如下所示:

     <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="\/MyControlsAssembly;component\/Themes\/generic.xaml"\/> <\/ResourceDictionary.MergedDictionaries> <\/ResourceDictionary> <\/Application.Resources><\/code><\/pre>

    then the custom controls are properly themed, but if I do not manually merge the generic.xaml, the controls appear with the default Windows theme.那么自定义控件的主题是正确的,但如果我不手动合并 generic.xaml,这些控件将显示为默认的 Windows 主题。

    Could you please tell me what am I forgetting and\/or doing wrong ?你能告诉我我忘记了什么和\/或做错了什么吗?

    Additional info:附加信息:

    • My ThemeInfo assembly attribute is defined as follow:我的 ThemeInfo 程序集属性定义如下:

      [assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)]<\/code>

      (Note: the result is just the same with any combination of parameters for the ThemeInfo attribute) (注意:对于 ThemeInfo 属性的任意参数组合,结果都是一样的)

      <\/li>

    • There are two others .xaml files beside the generic.xaml file in the Themes folder. Themes 文件夹中的 generic.xaml 文件旁边还有另外两个 .xaml 文件。

      <\/li>

    • There is a subfolder in the Themes folder that itself contains another .xaml file. Themes 文件夹中有一个子文件夹,它本身包含另一个 .xaml 文件。<\/li><\/ul>"

You need the following line in your custom control constructor: 您需要在自定义控件构造函数中使用以下行:

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

Then if you have a generic.xaml file inside themes folder, with the following sample style: 然后,如果在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>

Now the style will get automatically applied without any extra merging. 现在风格将自动应用,无需任何额外的合并。

To apply default styles in Themes\\Generic.xaml in my custom controls library I decided to seek inspiration from a well established open source controls library ( MahApps ). 要在我的自定义控件库中的Themes \\ Generic.xaml中应用默认样式,我决定从一个完善的开源控件库( MahApps )中寻找灵感。 This project gives you a really good example of how to structure and layout a custom controls library. 该项目为您提供了一个如何构建和布局自定义控件库的非常好的示例。

Cut a long story short, to get your default styles in Themes\\Generic.xaml you need to include the following in the AssemblyInfo.cs file in your custom controls library: 简而言之,要在Themes \\ Generic.xaml中获取默认样式,您需要在自定义控件库的AssemblyInfo.cs文件中包含以下内容:

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

In my case, my custom controls AssemblyInfo.cs looked something like: 在我的例子中,我的自定义控件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("...")]

Not sure if this works in WPF, but it works for me in Silverlight: 不确定这是否适用于WPF,但它在Silverlight中适用于我:

Constructor: 构造函数:

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

Style: 样式:

<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>

I was having the same problem due to the following attribute in the custom control assembly: 由于自定义控件程序集中的以下属性,我遇到了同样的问题:

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

Changing UltimateResourceFallbackLocation.Satellite to UltimateResourceFallbackLocation.MainAssembly or removing the second parameter completely fixed the problem for me (you can also remove the attribute if you do not need to define the neutral language of your assembly). UltimateResourceFallbackLocation.Satellite更改为UltimateResourceFallbackLocation.MainAssembly或删除第二个参数完全解决了我的问题(如果您不需要定义程序集的中性语言,也可以删除该属性)。

In My case I forget to remove the x:Key from <Style> tag.在我的情况下,我忘记从<Style>标记中删除x:Key

Wrong:错误的:

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

Correct:正确的:

<Style TargetType="controls:IpTextBox">

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

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