Make AppThemeBinding type public #22250
Replies: 10 comments 1 reply
-
@brminnick C# markup does have access to internal methods, right? If so, can we create a Factory method for |
Beta Was this translation helpful? Give feedback.
-
Yeah, it can even be in the Markup package as there's already an extension method available for AppThemeBinding (instance-based) but not for Setter. The return type has to be |
Beta Was this translation helpful? Give feedback.
-
We have extension methods, new Label()
.AppThemeBinding(Label.TextProperty, "Light Mode", "Dark Mode")
.AppThemeColorBinding(Label.TextColorProperty, Colors.Black, Colors.White);
var labelStyle = new Style<Label>()
.AddAppThemeBinding(Label.TextProperty, "Light Mode", "Dark Mode")
.AddAppThemeBinding(Label.TextColorProperty, Colors.Black, Colors.White);
new Label
{
Style = labelStyle
}; |
Beta Was this translation helpful? Give feedback.
-
Yes, but won't work in the context of Setter as there's no instance available to invoke this form of method. Style can be implicit also. Maybe will try with the another version updated in the earlier comment. |
Beta Was this translation helpful? Give feedback.
-
@brminnick, it works seamlessly with the generic version But one request, can another extension method be added to make it work with the non-generic version of Style to support the existing code base? // Since Style is sealed, have defined as it is
public static Style AddAppThemeBinding(this Style style, BindableProperty property, object light, object dark)
{
// Issue is AppThemeBinding is inaccessible due to its protection level
style.Setters.Add(property, new AppThemeBinding() { Light = light, Dark = dark });
return style;
} |
Beta Was this translation helpful? Give feedback.
-
@brminnick Or to make it simple, add another constructor to generic Since // Adding a condition to make it even more safer
public Style(Style style)
{
// Assign to the underlying property only if the Style's TargetType matches with that of the generic Type 'T'
if (typeof(T) == style.TargetType)
{
MauiStyle = style;
}
else
{
MauiStyle = new Style(typeof(T));
}
} |
Beta Was this translation helpful? Give feedback.
-
@egvijayanand I think that makes a lot of sense! Check out the proposal I just opened and leave a comment with your thoughts! |
Beta Was this translation helpful? Give feedback.
-
I found out that I can't access the It makes no sense that MAUI exists, but you need to install a community library to be able to bind to the app theme changes at all. And the community library still does not expose the You might say: But I can already bind to the AppTheme in XAML. Exactly! Why is it publicly accessible in one language (XAML) but not in C#? It is extremely weird. |
Beta Was this translation helpful? Give feedback.
-
This remains a valid request as I encounter issues defining the Visual States. Currently, no alternative method, even with the CommunityToolkit, which defines the I don't understand why there is a disparity in non-XAML development. |
Beta Was this translation helpful? Give feedback.
-
With regards to the Style, I believe @brminnick already answers this in his post #22250 (comment) which describes .AppThemeColorBinding() when applied to your example becomes: Resources.Add("MauiLabel", new Style<Label>()
.AddAppThemeBinding(Label.TextColorProperty, Colors.Black, Colors.White)); |
Beta Was this translation helpful? Give feedback.
-
Description
While working with C# Markup, the type
AppThemeBinding
is the equivalent ofAppThemeBindingExtension
from XAML.But this type is marked as internal. So, unable to make use of it while defining theme-based Resource definition (as an instance assigned to the value of the Setter).
For inline View definition, there's an extension method available. But not for Setter as there's no instance associated to invoke the extension method. Refer to the intended use case for the sample.
Public API Changes
Make the AppThemeBinding type public.
maui/src/Controls/src/Core/AppThemeBinding.cs
Line 9 in c3221bf
Intended Use-Case
Allows resource reuse.
Beta Was this translation helpful? Give feedback.
All reactions