Maui.Markup way to define VisualStates in Styles #64
devonuto
started this conversation in
New Feature Discussions
Replies: 5 comments 4 replies
|
This would be very helpful. As a reference, also look at VincentH-Net/CSharpForMarkup#22 |
0 replies
|
I agree, this would be very helpful. Do we create an issue from this to hopefully get it worked on? |
1 reply
|
Hello, any news about this question? Thanks. |
0 replies
|
this works: |
2 replies
|
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
);
} |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
All reactions