Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styles from generic.xaml are not applied

Tags:

I made a class library assembly in which I created custom controls, and I defined the default styles in the generic.xaml file.

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.
  • the generix.xaml file Build Action is set to Page.
  • the ThemeInfo is properly defined in my AssemblyInfo.cs.

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:

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

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.

Could you please tell me what am I forgetting and/or doing wrong ?

Additional info:

  • My ThemeInfo assembly attribute is defined as follow:

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

    (Note: the result is just the same with any combination of parameters for the ThemeInfo attribute)

  • There are two others .xaml files beside the generic.xaml file in the Themes folder.

  • There is a subfolder in the Themes folder that itself contains another .xaml file.
like image 811
Sebastien ROBERT Avatar asked Jun 22 '12 01:06

Sebastien ROBERT


People also ask

How do I add a style to XAML?

Apply a style implicitly You can change the default appearance by setting properties, such as FontSize and FontFamily, on each TextBlock element directly. However, if you want your TextBlock elements to share some properties, you can create a Style in the Resources section of your XAML file, as shown here.

What is generic XAML?

xaml file itself, the dictionary entry within generic. xaml can be a merged dictionary whose entries may reference anything.

What is app XAML used for?

In a Xamarin. Forms application, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.

Why XAML is used in WPF?

The goal of XAML is to enable visual designers to create user interface elements directly. WPF aims to make it possible to control all visual aspects of the user interface from mark-up.


1 Answers

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:

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

like image 90
Eternal21 Avatar answered Mar 13 '23 04:03

Eternal21