dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

Introduce the Generic.xaml concept #9612

Open dotMorten opened 2 years ago

dotMorten commented 2 years ago

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:

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 commented 2 years ago

related

https://github.com/dotnet/maui/issues/6350 https://github.com/dotnet/maui/issues/4132

ghost commented 2 years ago

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 commented 1 year ago

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

LanceMcCarthy commented 1 year ago

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

Alex-Dobrynin commented 1 year ago

+1 UP

mattleibow commented 1 year ago

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 commented 9 months ago

@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":

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 commented 1 month ago

Can we expect this for Net 9 version of Maui?