RadioButtons 是否应该在 VS 2022 预览版 17.1/MAUI 预览版 10 中工作?
Should RadioButtons work in VS 2022 Preview 17.1/MAUI preview 10?
我是 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");
}
}
}
我正在尝试 运行 它安装在 Windows 10 系统 Visual Studio 上。
代码基本上是 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”功能目前不受支持或已损坏?
不幸的是毛伊岛的发布日期被推迟了。因此,它目前仍处于未来几个月的预览阶段。部分原因是某些控制措施尚未完全发挥作用或仍在进行中。
有这个官方 status page,您可以在其中找到每个组件的当前状态,在撰写本文时,RadioButton
仍然是缺失的部分之一。
Maui 目前不应用于任何生产工作负载。我也开始使用 Maui 并进行试验。我已经遇到过许多控件或调试器本身的错误,我知道它会变得非常令人沮丧。
如果你想 learn/experiment/play 没关系,但如果你想要一个有效的跨平台项目,你应该等待正式发布(应该是 2022 年第二季度)或坚持使用 Xamarin Forms。
我是 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");
}
}
}
我正在尝试 运行 它安装在 Windows 10 系统 Visual Studio 上。
代码基本上是 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”功能目前不受支持或已损坏?
不幸的是毛伊岛的发布日期被推迟了。因此,它目前仍处于未来几个月的预览阶段。部分原因是某些控制措施尚未完全发挥作用或仍在进行中。
有这个官方 status page,您可以在其中找到每个组件的当前状态,在撰写本文时,RadioButton
仍然是缺失的部分之一。
Maui 目前不应用于任何生产工作负载。我也开始使用 Maui 并进行试验。我已经遇到过许多控件或调试器本身的错误,我知道它会变得非常令人沮丧。
如果你想 learn/experiment/play 没关系,但如果你想要一个有效的跨平台项目,你应该等待正式发布(应该是 2022 年第二季度)或坚持使用 Xamarin Forms。