I am new to MAUI but can't get a simple radio button sample (converted from the Xamarin.Forms control gallery) to work, the project builds fine but on running it I get TypeInitializationException: The type initializer for 'Microsoft.Maui.Controls.RadioButton' threw an exception
with a long stack trace:
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=System.Private.CoreLib
StackTrace:
at System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized)
at System.Reflection.RtFieldInfo.GetValue(Object obj)
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetBindableProperty(Type elementType, String localName, IXmlLineInfo lineInfo, Boolean throwOnError)
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.TrySetPropertyValue(Object element, XmlName propertyName, String xKey, Object value, Object rootElement, IXmlLineInfo lineInfo, IServiceProvider serviceProvider, Exception& xpe)
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo)
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(ValueNode node, INode parentNode)
at Microsoft.Maui.Controls.Xaml.ValueNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties)
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType)
at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[TXaml](TXaml view, Type callingType)
at MAUIRadioButtonExampleXamFormsGallery.MainPage.InitializeComponent() in C:\Users\marti\source\repos\MAUIRadioButtonExampleXamFormsGallery\MAUIRadioButtonExampleXamFormsGallery\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\MainPage.xaml.sg.cs:line 16
at MAUIRadioButtonExampleXamFormsGallery.MainPage..ctor() in C:\Users\marti\source\repos\MAUIRadioButtonExampleXamFormsGallery\MAUIRadioButtonExampleXamFormsGallery\MainPage.xaml.cs:line 12
at MAUIRadioButtonExampleXamFormsGallery.App..ctor() in C:\Users\marti\source\repos\MAUIRadioButtonExampleXamFormsGallery\MAUIRadioButtonExampleXamFormsGallery\App.xaml.cs:line 14
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.Maui.MauiWinUIApplication.OnLaunched(LaunchActivatedEventArgs args)
at MAUIRadioButtonExampleXamFormsGallery.WinUI.App.OnLaunched(LaunchActivatedEventArgs args) in C:\Users\marti\source\repos\MAUIRadioButtonExampleXamFormsGallery\MAUIRadioButtonExampleXamFormsGallery\Platforms\Windows\App.xaml.cs:line 29
at Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml.IApplicationOverrides.OnLaunched(LaunchActivatedEventArgs args)
at ABI.Microsoft.UI.Xaml.IApplicationOverrides.Do_Abi_OnLaunched_0(IntPtr thisPtr, IntPtr args)
It is thrown in a (generated?) file MainPage.xaml.sg.cs
on the statement global::Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(this, typeof(MainPage));
in:
[assembly: global::Microsoft.Maui.Controls.Xaml.XamlResourceId("MAUIRadioButtonExampleXamFormsGallery.MainPage.xaml", "MainPage.xaml", typeof(global::MAUIRadioButtonExampleXamFormsGallery.MainPage))]
namespace MAUIRadioButtonExampleXamFormsGallery
{
[global::Microsoft.Maui.Controls.Xaml.XamlFilePath("MainPage.xaml")]
public partial class MainPage : global::Microsoft.Maui.Controls.ContentPage
{
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Maui.Controls.SourceGen", "1.0.0.0")]
private global::Microsoft.Maui.Controls.Label colorLabel;
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Maui.Controls.SourceGen", "1.0.0.0")]
private global::Microsoft.Maui.Controls.Label fruitLabel;
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Maui.Controls.SourceGen", "1.0.0.0")]
private void InitializeComponent()
{
global::Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(this, typeof(MainPage));
colorLabel = global::Microsoft.Maui.Controls.NameScopeExtensions.FindByName<global::Microsoft.Maui.Controls.Label>(this, "colorLabel");
fruitLabel = global::Microsoft.Maui.Controls.NameScopeExtensions.FindByName<global::Microsoft.Maui.Controls.Label>(this, "fruitLabel");
}
}
}
I am trying to run it on the Windows 10 system Visual Studio is installed on.
Code is basically the template that is created by Visual Studio 2022 Preview 17.1 with the two files for the MainPage being edited to be as follows:
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Essentials;
namespace MAUIRadioButtonExampleXamFormsGallery
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
void OnColorsRadioButtonCheckedChanged(object sender, CheckedChangedEventArgs e)
{
RadioButton button = sender as RadioButton;
colorLabel.Text = $"You have chosen: {button.Content}";
}
void OnFruitsRadioButtonCheckedChanged(object sender, CheckedChangedEventArgs e)
{
RadioButton button = sender as RadioButton;
fruitLabel.Text = $"You have chosen: {button.Content}";
}
}
}
and
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUIRadioButtonExampleXamFormsGallery.MainPage"
BackgroundColor="{DynamicResource SecondaryColor}"
Title="RadioButton Example">
<StackLayout Margin="10">
<Label Text="RadioButton"
FontSize="50"
FontAttributes="Bold"
HorizontalOptions="Center" />
<Label Text="What's your favorite color?" />
<RadioButton Content="Red"
TextColor="Red"
GroupName="colors"
CheckedChanged="OnColorsRadioButtonCheckedChanged" />
<RadioButton Content="Green"
TextColor="Green"
GroupName="colors"
CheckedChanged="OnColorsRadioButtonCheckedChanged" />
<RadioButton Content="Blue"
TextColor="Blue"
GroupName="colors"
CheckedChanged="OnColorsRadioButtonCheckedChanged" />
<RadioButton Content="Other"
GroupName="colors"
CheckedChanged="OnColorsRadioButtonCheckedChanged" />
<Label x:Name="colorLabel"
Text="You have chosen:" />
<Label Text="What's your favorite fruit?" />
<RadioButton Content="Apple"
GroupName="fruits"
CheckedChanged="OnFruitsRadioButtonCheckedChanged" />
<RadioButton Content="Banana"
GroupName="fruits"
CheckedChanged="OnFruitsRadioButtonCheckedChanged" />
<RadioButton Content="Pineapple"
GroupName="fruits"
CheckedChanged="OnFruitsRadioButtonCheckedChanged" />
<RadioButton Content="Other"
GroupName="fruits"
CheckedChanged="OnFruitsRadioButtonCheckedChanged" />
<Label x:Name="fruitLabel"
Text="You have chosen:" />
</StackLayout>
</ContentPage>
Am I doing something completely wrong or is that feature "RadioButton" currently not supported or broken?
CodePudding user response:
Unfortunately Maui's release date has been delayed. So it's currently still in preview for the next few months. Part of the reasons are that some controls are not fully working yet or still underway.
Maui should not be used for any production workload at the moment. I've also started using Maui and experimenting. I already faced many bugs with controls or with the debugger itself and I know it can get very frustrating.
If you want to learn/experiment/play around that's fine, but if you want a working cross platform project you should either wait for the official release (should be Q2 2022) or stick to Xamarin Forms.