Introduce the Generic.xaml concept
See original GitHub issueDescription
As a control developer I often write controls that have templates. Templates are great because it allows the developer to overwrite the styles easily. However templating custom controls in MAUI is surprisingly hard compared to WPF, UWP and WinUI.
In those frameworks all you need to do is include a Themes\Generic.xaml
file with the style, and in each control set the DefaultStyleKey
property. At this point the Generic.xaml styles are automatically loaded and applied to these controls, unless the developer chooses to overwrite these styles. It makes for a very clean story for creating custom control libraries.
Public API Changes
public class MyControl : TemplatedView
{
public MyControl()
{
DefaultStyleKey = typeof(MyControl);
}
}
Intended Use-Case
As a control developer, I want to build class libraries with custom controls in it, and these controls inherit from TemplatedView. I want to use the XAML editor/designer to define the styles, and I want to make it easy for end developers to also overwrite these styles.
Coming from the other XAML platforms, we’re used to do this with the Generic.xaml
file in the Themes
folder. A similar concept is missing from .NET MAUI.
There we’re forced to either:
- manually load it in the constructor from a string (slow) example
- ask the end user to add a resource dictionary to their application (error-prone / cumbersome)
- On-the-fly insert our own resource dictionary into the application resources, which is error prone (could be added in the wrong order and override user-defined styles, and it feels wrong for a control to be modifying application resources). example - literally called a hack
Another great benefit is in Visual Studio I can right-click a control and choose to create a copy of the default style - I’ll get the style defined in my app or page based on the original style, and I’ll have a good starting point to modify the original template.
Issue Analytics
- State:
- Created a year ago
- Reactions:18
- Comments:5 (1 by maintainers)
Please, make this high priority. Currently, component authors have to resort to ugly hacks to provide some default styles and theming for the controls.
+1 UP