Maui.Markup way to define VisualStates in Styles #64
Replies: 5 comments 4 replies
-
|
This would be very helpful. As a reference, also look at VincentH-Net/CSharpForMarkup#22 |
Beta Was this translation helpful? Give feedback.
-
|
I agree, this would be very helpful. Do we create an issue from this to hopefully get it worked on? |
Beta Was this translation helpful? Give feedback.
-
|
Hello, any news about this question? Thanks. |
Beta Was this translation helpful? Give feedback.
-
|
this works: |
Beta Was this translation helpful? Give feedback.
-
|
By sheer coincidence I wrote a set of extensions yesterday, before I learned of the DISCLAIMER: Not rigorously tested yet!static class ResourceDictionaryExtensions
{
public static Style<T> AddStyle<T>(this ResourceDictionary self)
where T : BindableObject => self.AddNamedStyle<T>(typeof(T).FullName!);
public static Style<T> AddNamedStyle<T>(this ResourceDictionary self, string key)
where T : BindableObject
{
var style = new Style<T>();
self.Add(key, style.MauiStyle);
return style;
}
}
static class StyleExtensions
{
public static Style<T> WithVisualStates<T>(this Style<T> self, params IEnumerable<VisualStateSetters> stateConfigs)
where T : BindableObject => WithNamedVisualStates(self, "CommonStates", stateConfigs);
public static Style<T> WithNamedVisualStates<T>(
this Style<T> self,
string stateGroupName,
params IEnumerable<VisualStateSetters> stateConfigs
)
where T : BindableObject
{
if (
self.MauiStyle.Setters.FirstOrDefault(setter => setter.Property == VisualStateManager.VisualStateGroupsProperty)
is not { Value: VisualStateGroupList groupList }
)
{
self.MauiStyle.Setters.Add(
new() { Property = VisualStateManager.VisualStateGroupsProperty, Value = groupList = [] }
);
}
var group = new VisualStateGroup { Name = stateGroupName };
foreach (var (stateName, setters) in stateConfigs)
{
var state = new VisualState { Name = stateName };
foreach (var (property, value) in setters)
{
state.Setters.Add(new Setter { Property = property, Value = value });
}
group.States.Add(state);
}
groupList.Add(group);
return self;
}
public sealed record VisualStateSetters(
string StateName,
params IEnumerable<(BindableProperty Property, object Value)> Setters
);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Working with the new Maui.Markup way of defining style setters:
It would be nice if we could extend it for adding visual states, if possible maybe something simple like this:
May obviously need to change, if multiple state groups are required, but this method would assume all visual states are within the same visual state group.
Currently, the only method I have found to define these state setters is by creating my own Grid type like this which is a little convoluted:
Related origin: #61
Beta Was this translation helpful? Give feedback.
All reactions