Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the Generic.xaml concept #9612

Open
dotMorten opened this issue Aug 23, 2022 · 8 comments
Open

Introduce the Generic.xaml concept #9612

dotMorten opened this issue Aug 23, 2022 · 8 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors partner Issue or Request from a partner team proposal/open
Milestone

Comments

@dotMorten
Copy link
Contributor

dotMorten commented Aug 23, 2022

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

@PureWeen
Copy link
Member

related

#6350
#4132

@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Aug 24, 2022
@PureWeen PureWeen added this to the Backlog milestone Aug 25, 2022
@ghost
Copy link

ghost commented Aug 25, 2022

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@vanka78bg
Copy link

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

@jsuarezruiz jsuarezruiz added the partner Issue or Request from a partner team label Feb 16, 2023
@LanceMcCarthy
Copy link

Bump 👍🏼 Are there any branches this is being worked on that I can follow/experiment with?

@Alex-Dobrynin
Copy link
Contributor

+1 UP

@mattleibow
Copy link
Member

I don't think we have started any work on this. However, one issue to deal with is the "scanning" part.

  1. If this happens at runtime, we have to load all assemblies which is not what we want to do I am thinking?
  2. If we do it at compile time, then we have to scan all assemblies and add references - but then we now reference everything and the linker will be leaving more things behind.

However, I think option 2 is a bit better since it is faster for startup and the code can all be generated.

@StephaneDelcroix are there any features that we have today that will do things for us or make things easier? Is there a way to hook into the xaml build?

@panayot-cankov
Copy link

panayot-cankov commented Dec 18, 2023

@mattleibow There are more details in how Generic.xaml worked in WPF

"DefaultStyleKey" would be used to look up a "default style" in WPF. Its values would apply with lower precedence than an "implicit style".

"ResourceKey" could be created by a control instance for its "DefaultStyleKey", and the "ResourceKey" has an Assembly property. When WPF would lookup Generic.xaml, it will do so in the Assembly specified by that property. That determines which assemblies have to be scanned for "Generic.xaml".

The above two features in combination, allowed for Control developers to ship "theme assemblies":

  • Application developers could select a theme per vendor based on user settings
  • The theme could change at runtime, changing the "default style"
  • The application developer's implicit styles would still apply on top of default (theme) styles

Linker is a bummer, for .NET MAUI it would be awesome if we could still ship multiple assemblies with themes containing Generic.xaml but have them stripped from unused styles after the linking phase.

@AathifMahir
Copy link

Can we expect this for Net 9 version of Maui?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors partner Issue or Request from a partner team proposal/open
Projects
None yet
Development

No branches or pull requests

9 participants