0

我是 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”?

4

1 回答 1

2

不幸的是,毛伊岛的发布日期被推迟了。因此,它目前仍处于未来几个月的预览阶段。部分原因是某些控制措施尚未完全发挥作用或仍在进行中。

有这个官方状态页面,您可以在其中找到每个组件的当前状态,在撰写本文时,RadioButton仍然是缺失的部分之一。

在此处输入图像描述

目前不应将 Maui 用于任何生产工作负载。我也开始使用毛伊岛并进行实验。我已经遇到了许多控件或调试器本身的错误,我知道它会变得非常令人沮丧。

如果您想学习/实验/玩耍,那很好,但如果您想要一个有效的跨平台项目,您应该等待正式发布(应该是 2022 年第二季度)或坚持使用 Xamarin Forms。

于 2021-11-12T03:45:21.927 回答