-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Style values are being applied too early #14678
Comments
@StephaneDelcroix Is this work that #13818 would handle? |
All is applied in the correct order. you should apply the Style on a fully build control, by doing |
So if I understand you correctly, we shouldn't be specifying the style inside the xaml of the control itself, but outside on the page/parent control instead? Is that right? |
@StephaneDelcroix I should note that this is functionality that was working in Xamarin Forms. We migrated our code to .NET MAUI, and now it's broken. You can see a possible workaround in my public repo, but it's pretty ugly so we'd rather not have to do that. |
another workaround would be to apply the Style at the end of the XAML (as element value, not attribute on the top). it's still a workaround, you should probably stick to wha tI said earlier, apply Style on fully build controls |
Verified this on Visual Studio Enterprise 17.7.0 Preview 2.0. Repro on Windows 11, Android 13.0-API33 and iOS 16.4 with below Project: |
This is also an issue with CollectionViews on Android. If you add a x:Name to a collection view, headers and empty views appear off centred. The empty view will be all the way to the right and cut-off. |
need to revisit this now that the SetterSpecificity branch was merged |
I revisited this issue, and everything works as expected. you definitely should not apply the style to the control being build, that would not work in c# either. |
Description
Styles seem to be applied before Elements with
x:Name
values have been initialized by MAUI. This can cause aNullReferenceException
and other undesired effects. This is a common use case for us (same control with multiple Styles, for example), and it causes our styling to be wrong (or instability if we aren't null checking).See my public repo for screenshots
Steps to Reproduce
If starting from scratch:
Label
with anx:Name
valueText
bindable property which sets theLabel
'sText
in the code behind (make sure you null check theLabel
)Style
in Styles.xaml with aTargetType
that matches your control, add a setter for the aboveText
bindable property and choose a unique stringStyle
has aKey
Style
'sKey
in your custom controlLabel
is null when it's referenced by itsx:Name
Link to public reproduction project repository
https://github.com/mavispuford/MauiStyleValuesAppliedTooEarly
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
iOS, Android, Windows, I was not able test on other platforms
Affected platform versions
Android 13, iOS 16.2, Windows 10.0.19044 Build 19044
Did you find any workaround?
Yes (it's not pretty) - See my public repo. I basically do some null checks/Task.Delay() calls in a Task.Run(), then I set the value. Another (untested) possible solution could be to do a Task.Run() (not awaited) after
InitializeComponent()
in the constructor, and set the control's Style value after a Task.Delay().Relevant log output
No response
The text was updated successfully, but these errors were encountered: