RadioButtons 是不是应该在 VS 2022 Preview 17.1/MAUI preview 10 中工作?
Posted
技术标签:
【中文标题】RadioButtons 是不是应该在 VS 2022 Preview 17.1/MAUI preview 10 中工作?【英文标题】:Should RadioButtons work in VS 2022 Preview 17.1/MAUI preview 10?RadioButtons 是否应该在 VS 2022 Preview 17.1/MAUI preview 10 中工作? 【发布时间】:2021-12-24 05:09:30 【问题描述】:我是 MAUI 的新手,但无法使用简单的单选按钮示例(从 Xamarin.Forms 控件库转换)来工作,该项目构建良好,但在运行它时我得到 TypeInitializationException: The type initializer for 'Microsoft.Maui.Controls.RadioButton' threw an exception
并带有很长的堆栈跟踪:
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)
它被扔在一个(生成的?)文件MainPage.xaml.sg.cs
中的声明global::Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(this, typeof(MainPage));
中:
[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");
我正在尝试在安装了 Visual Studio 的 Windows 10 系统上运行它。
代码基本上是由 Visual Studio 2022 Preview 17.1 创建的模板,其中 MainPage 的两个文件被编辑如下:
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";
和
<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>
我是在做完全错误的事情,还是当前不支持或损坏了“RadioButton”功能?
【问题讨论】:
【参考方案1】:很遗憾,毛伊岛的发布日期被推迟了。因此,它目前仍处于未来几个月的预览阶段。部分原因是某些控制措施尚未完全发挥作用或仍在进行中。
有这个官方status page,您可以在其中找到每个组件的当前状态,在撰写本文时,RadioButton
仍然是缺失的部分之一。
目前不应将 Maui 用于任何生产工作负载。我也开始使用毛伊岛并进行实验。我已经遇到过很多关于控件或调试器本身的错误,我知道这会让人非常沮丧。
如果您想学习/实验/玩耍,这很好,但如果您想要一个有效的跨平台项目,您应该等待正式发布(应该是 2022 年第二季度)或坚持使用 Xamarin Forms。
【讨论】:
以上是关于RadioButtons 是不是应该在 VS 2022 Preview 17.1/MAUI preview 10 中工作?的主要内容,如果未能解决你的问题,请参考以下文章
使用 radioButtons 在 silverlight (xaml) 中选择字母字符
Knockout + Radiobuttons - 在取消选中时将值切换回“false”
GroupBox 与 radioButtons 启用另一个 groupBox 不保持按钮状态