maui icon indicating copy to clipboard operation
maui copied to clipboard

Style applied to a wrong element throws exception in Release but not in Debug

Open czmirek opened this issue 4 months ago • 2 comments

Description

Invalid XAML throws exception in Release but not in Debug. It should be thrown in both or better it should become a compilation error.

The content page below contains a Border that has a Style="{StaticResource BlackButton}".

But the BlackButton is targetted only at Buttons. This works perfectly fine in Debug but throws an exception in Release. This makes it difficult to spot when you make this mistake.

TestPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiReleaseXamlBug"
             x:Class="MauiReleaseXamlBug.TestPage"
             x:DataType="local:TestViewModel"
             Title="TestPage">
    <Border Style="{StaticResource BlackButton}">
    </Border>
</ContentPage>

App.xaml

<Style TargetType="Button" x:Key="BaseButton">
    <Setter Property="CornerRadius" Value="25" />
    <Setter Property="Padding" Value="20,10,20,10" />
    <Setter Property="FontSize" Value="16" />
    <Setter Property="HeightRequest" Value="48" />
</Style>

<Style TargetType="Button" x:Key="BlackButton" BasedOn="{StaticResource BaseButton}">
    <Setter Property="BackgroundColor" Value="#000000" />
    <Setter Property="TextColor" Value="#FFFFFF" />
</Style>

See log output below.

dotnet workload list

Installed Workload Id      Manifest Version       Installation Source                
-------------------------------------------------------------------------------------
maui-windows               8.0.7/8.0.100          VS 17.9.34701.34, VS 17.10.34607.79
android                    34.0.79/8.0.100        VS 17.9.34701.34, VS 17.10.34607.79
maccatalyst                17.2.8022/8.0.100      VS 17.9.34701.34, VS 17.10.34607.79
ios                        17.2.8022/8.0.100      VS 17.9.34701.34, VS 17.10.34607.79

Steps to Reproduce

  • Checkout the attached repository https://github.com/czmirek/MauiReleaseXamlBug
  • Run project in Debug
  • Click on "Open page"
  • Everything works, page opens
  • Close project
  • Run again in Release
  • Exception is thrown --- to avoid the app crashing, the exception is caught and shown in dialog.

Link to public reproduction project repository

https://github.com/czmirek/MauiReleaseXamlBug

Version with bug

Unknown/Other

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

Android 14, iOS 17 (iPhone 12)

Did you find any workaround?

Simply fix the style: make duplicate style to target the Border element or remove the style and apply it directly.

Relevant log output

System.InvalidCastException: Arg_InvalidCastException
at Microsoft.Maui.Controls.FontElement.FontSizeDefaultValueCreator(BindableObject bindable)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Setter.Apply(BindableObject target, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Style.ApplyCore(BindableObject bindable, Style basedOn, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Style.Microsoft.Maui.Controls.IStyle.Apply(BindableObject bindable, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Style.ApplyCore(BindableObject bindable, Style basedOn, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Style.Microsoft.Maui.Controls.IStyle.Apply(BindableObject bindable, SetterSpecificity specificity)
at Microsoft.Maui.Controls.MergedStyle.SetStyle(IStyle implicitStyle, IList`1 classStyles, IStyle style)
at Microsoft.Maui.Controls.MergedStyle.set_Style(IStyle value)
at Microsoft.Maui.Controls.NavigableElement.<>c.<.cctor>b__22_0(BindableObject bindable, Object oldvalue, Object newvalue)   
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
at Microsoft.Maui.Controls.NavigableElement.set_Style(Style value)
at MauiReleaseXamlBug.TestPage.InitializeComponent()
at MauiReleaseXamlBug.TestPage..ctor()
at MauiReleaseXamlBug.MainPage.Button_Clicked(Object sender, EventArgs e)

czmirek avatar Apr 05 '24 15:04 czmirek

Can repro this issue at Android platform on the latest 17.10 preview 2(8.0.10&8.0.14). 21661

RoiChen001 avatar Apr 07 '24 01:04 RoiChen001

I just had the same problem with a Style targeted for a Label type and being applied (accidently) to a VerticalStackLayout.

Is there not a tool that can walk the XAML and styles and report the errors at compile time?

joshball avatar Apr 11 '24 18:04 joshball