MaterialButton: XPE when loading styles with triggers
🔙 Regression
In our Application we use MaterialButtons to emulate toggle buttons. The default ButtonType is Text.
For Buttons that are toggleable Buttons the ButtonType is set via a Binding and a custom BoolToButtonTypeConverter.
(false->Text, true->Flat)
To invert the TextColor when the Button is toggled on, we use the following style, defined in App.xaml.
<Style TargetType="ui:MaterialButton" ApplyToDerivedTypes="False">
<Style.Triggers>
<Trigger TargetType="ui:MaterialButton" Property="ButtonType" Value="Flat">
<Trigger.Setters>
<Setter Property="TextColor" Value="White"/>
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
Old (and correct) behavior
The app is initialized without any errors, Buttons behave as expected. (Xamarin.Forms 4.7.0.1142; XF.Material 1.5.8)
Current behavior
The app fails to start with a XamlParseException, as soon as the App-Constructor calls InitializeComponent(). (Xamarin.Forms 5.0.0.1874; XF.Material 1.7.7)
Callstack:
Xamarin.Forms.Xaml.XamlParseException: Position 62:53. Type converter failed: The type initializer for 'XF.Material.Forms.UI.MaterialButton' threw an exception. ---> System.TypeInitializationException: The type initializer for 'XF.Material.Forms.UI.MaterialButton' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object
at XF.Material.Forms.Material.GetResource[T] (System.String key) [0x00000] in C:\Users\mhvdi\Documents\OpenSource\XF-Material-Library\XF.Material\Material.cs:57
at XF.Material.Forms.Material+Color.GetSecondaryColor () [0x00000] in C:\Users\mhvdi\Documents\OpenSource\XF-Material-Library\XF.Material\Material.cs:203
at XF.Material.Forms.Material+Color.get_Secondary () [0x00000] in C:\Users\mhvdi\Documents\OpenSource\XF-Material-Library\XF.Material\Material.cs:198
at XF.Material.Forms.UI.MaterialButton..cctor () [0x0003e] in C:\Users\mhvdi\Documents\OpenSource\XF-Material-Library\XF.Material\UI\MaterialButton.cs:20
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Reflection.RuntimeFieldInfo.GetValueInternal(System.Reflection.RuntimeFieldInfo,object)
at System.Reflection.RuntimeFieldInfo.GetValue (System.Object obj) [0x0005e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeFieldInfo.cs:251
at Xamarin.Forms.BindablePropertyConverter.ConvertFrom (System.Type type, System.String propertyName, System.Xml.IXmlLineInfo lineinfo) [0x00055] in D:\a\1\s\Xamarin.Forms.Core\BindablePropertyConverter.cs:99
at Xamarin.Forms.BindablePropertyConverter.Xamarin.Forms.IExtendedTypeConverter.ConvertFromInvariantString (System.String value, System.IServiceProvider serviceProvider) [0x00169] in D:\a\1\s\Xamarin.Forms.Core\BindablePropertyConverter.cs:60
at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`1[TResult] getConverter, System.IServiceProvider serviceProvider, System.Exception& exception) [0x00041] in D:\a\1\s\Xamarin.Forms.Core\Xaml\TypeConversionExtensions.cs:134
--- End of inner exception stack trace ---
at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue (System.Object xamlelement, Xamarin.Forms.Xaml.XmlName propertyName, System.Object value, System.Object rootElement, Xamarin.Forms.Xaml.INode node, Xamarin.Forms.Xaml.HydrationContext context, System.Xml.IXmlLineInfo lineInfo) [0x0007d] in D:\a\1\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:366
at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ValueNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0007b] in D:\a\1\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:60
at Xamarin.Forms.Xaml.ValueNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00000] in D:\a\1\s\Xamarin.Forms.Xaml\XamlNode.cs:91
at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00044] in D:\a\1\s\Xamarin.Forms.Xaml\XamlNode.cs:152
at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00044] in D:\a\1\s\Xamarin.Forms.Xaml\XamlNode.cs:152
at Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x000ec] in D:\a\1\s\Xamarin.Forms.Xaml\FillResourceDictionariesVisitor.cs:57
at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00012] in D:\a\1\s\Xamarin.Forms.Xaml\XamlNode.cs:147
at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00079] in D:\a\1\s\Xamarin.Forms.Xaml\XamlNode.cs:211
at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydrationContext visitorContext, System.Boolean useDesignProperties) [0x0007e] in D:\a\1\s\Xamarin.Forms.Xaml\XamlLoader.cs:235
at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml, System.Reflection.Assembly rootAssembly, System.Boolean useDesignProperties) [0x000a2] in D:\a\1\s\Xamarin.Forms.Xaml\XamlLoader.cs:104
at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml, System.Reflection.Assembly rootAssembly) [0x00000] in D:\a\1\s\Xamarin.Forms.Xaml\XamlLoader.cs:78
at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.String xaml, System.Reflection.Assembly rootAssembly) [0x00000] in D:\a\1\s\Xamarin.Forms.Xaml\ViewExtensions.cs:49
at Xamarin.Forms.Xaml.ResourcesLoader.CreateFromResource[T] (System.String resourcePath, System.Reflection.Assembly assembly, System.Xml.IXmlLineInfo lineInfo) [0x000b1] in D:\a\1\s\Xamarin.Forms.Xaml\ResourcesLoader.cs:39
at Xamarin.Forms.ResourceDictionary.SetAndLoadSource (System.Uri value, System.String resourcePath, System.Reflection.Assembly assembly, System.Xml.IXmlLineInfo lineInfo) [0x00063] in D:\a\1\s\Xamarin.Forms.Core\ResourceDictionary.cs:76
at Vendor.MobileApp.App.InitializeComponent ()
Reproduction steps
Add the style above into App.xaml and try to launch the App in an Emulator.
Configuration
Version: 1.7.7
Platform:
- [x] :iphone: iOS
- [x] :robot: Android
- [ ] :checkered_flag: WPF
- [ ] :earth_americas: UWP
- [ ] :apple: MacOS
- [ ] :tv: tvOS
- [x] :monkey: Xamarin.Forms
Same issue with MaterialTextField styles. I managed to get it working by placing XF.Material.Forms.Material.Init(this); before InitializeComponent(); in App.xaml.cs