我是 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”功能?
uj5u.com熱心網友回復:
不幸的是,毛伊島的發布日期被推遲了。因此,它目前仍處于未來幾個月的預覽階段。部分原因是某些控制尚未完全發揮作用或仍在進行中。
有這個官方
Maui 目前不應用于任何生產作業負載。我也開始使用 Maui 并進行試驗。我已經遇到了許多與控制元件或除錯器本身有關的錯誤,我知道它會變得非常令人沮喪。
如果你想學習/實驗/玩,那很好,但如果你想要一個作業的跨平臺專案,你應該等待官方發布(應該是 2022 年第二季度)或堅持使用 Xamarin Forms。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/358456.html
