Skip to content
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

Fix OnPlatform + Setter when no match for current platform #17061

Merged
merged 3 commits into from Feb 12, 2024

Conversation

BretJohnson
Copy link
Member

When OnPlatforfm is used for a Setter value, and there's no match for the current platform, fix so that the property isn't set (same behavior as Xamarin.Forms). Previously, XamlC would report a "Missing Value For Setter" error when this happens and non-compiled XAML would crash with an NRE. This PR contains fixes for both, ensuring the XAML compiler doesn't show an error, then ensuring it doesn't crash at runtime.

Fixes #12064

When OnPlatforfm is used for a Setter value, and there's no match for the
current platform, fix so that the property isn't set (same behavior as Xamarin.Forms).
Previously, XamlC would report a "Missing Value For Setter" error when this happens
and non-compiled XAML would crash with an NRE. This PR contains fixes for both,
ensuring the XAML compiler doesn't show an error, then ensuring it doesn't crash
at runtime.

Fixes #12064
@mattleibow
Copy link
Member

mattleibow commented Sep 5, 2023

I think this needs to be a XAML test as well so that we can ensure the XamlC and XamlG code is the same.

Maybe this file: https://github.com/dotnet/maui/blob/main/src/Controls/tests/Xaml.UnitTests/OnPlatformTests.cs

@BretJohnson
Copy link
Member Author

What do you mean by a XAML test? You mean a non compiled XAML test in addition to the compiled XAML test that's there currently?

@mattleibow
Copy link
Member

Oops, I linked the wrong file, but there are 2 types of xaml tests - and this is the one that I was actually talking about: https://github.com/dotnet/maui/blob/main/src/Controls/tests/Xaml.UnitTests/OnPlatform.xaml.cs

@mattleibow
Copy link
Member

You may just be able to add to that test... It seems to have a bunch of things you are doing.

{
public Issue12604()
{
InitializeComponent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please look at the other tests, to see how we test for both runtime and compiled Xaml

if (valueNode == null)
throw new XamlParseException("Missing Value for Setter", (IXmlLineInfo)node);
yield break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this will have effects outside of OnPlatform

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StephaneDelcroix - Yes, this change can have effects outside of OnPlatform, but previously those cases failed with the exception here, so the risk of breaking anything, making things worse, seems small - it previously just error'ed out. That said, if you can tell me how this code is actually used outside of OnPlatform, like suggest some test scenarios, I can test those.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property would not be set, right?. So for example, here:

<Style x:Key="ButtonStyle0" TargetType="Button">
    <Setter Property="FontSize" Value="{OnPlatform iOS=36}" />
</Style>

The FontSize property on Android, would have the default value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

@BretJohnson
Copy link
Member Author

You may just be able to add to that test... It seems to have a bunch of things you are doing.

I updated, moving all my tests there, which also covers testing compiled and non-compiled XAML.

@BretJohnson
Copy link
Member Author

This is ready to merge, from my perspective, once @StephaneDelcroix approves (or suggests additional test cases).

@BretJohnson
Copy link
Member Author

@StephaneDelcroix ping on this; can you take a look; thanks

@samhouts samhouts added the stale Indicates a stale issue/pr and will be closed soon label Nov 21, 2023
@PureWeen PureWeen removed this from the .NET 8 SR2 milestone Jan 16, 2024
@rmarinho rmarinho merged commit 7b07a83 into main Feb 12, 2024
47 checks passed
@rmarinho rmarinho deleted the fix-on-platform-no-match branch February 12, 2024 19:20
@github-actions github-actions bot locked and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors stale Indicates a stale issue/pr and will be closed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setter showing a Build Error when using XAML OnPlatform Markup Extension
8 participants