question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce the Generic.xaml concept

See original GitHub issue

Description

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:18
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
vanka78bgcommented, Feb 16, 2023

Please, make this high priority. Currently, component authors have to resort to ugly hacks to provide some default styles and theming for the controls.

1reaction
Alex-Dobrynincommented, Jul 5, 2023

+1 UP

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generics in XAML
In XAML, a generic type must always be represented as a constrained generic. An unconstrained generic is never present in the XAML type ......
Read more >
XAML theme resources - Windows apps
Theme resources in XAML are a set of resources that apply different values depending on which system theme is active.
Read more >
What is so special about Generic.xaml?
xaml, on XP using the default theme it is Luna.NormalColor.xaml. If the style is not found in the theme dictionary, it looks in...
Read more >
Structuring Your XAML Sources
Introduction. When creating custom controls in Visual Studio, all controls are added to /Themes/Generic.xaml. Soon this file becomes quite ...
Read more >
Understanding Styles in XAML based Applications
XAML controls adhere to the concept of lookless controls, which essentially means that the behaviour is independent of its styling. In the case ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found