如何解决Xamarin.Forms中App.xaml.cs的System.TypeLoadException异常?
Hey there, let's work through this TypeLoadException you're facing when launching your Xamarin.Forms app. The error indicates the runtime can't find the VisualDiagnostics class from Xamarin.Forms.Core—this is almost always tied to version mismatches, corrupted build artifacts, or misconfigured NuGet packages. Here are actionable steps to fix this:
Ensure consistent Xamarin.Forms versions across all projects
Mismatched package versions between your shared project, Android, and iOS projects are a common culprit. Head to your solution's NuGet package manager (right-click solution > Manage NuGet Packages for Solution) and verify that every project has the exact same version of Xamarin.Forms installed. Even minor version differences can break type resolution.Clean and rebuild your solution thoroughly
Cached build files often cause odd runtime errors. Try this sequence:- Right-click your solution and select "Clean Solution"
- Manually delete the
binandobjfolders from your shared, Android, and iOS projects - Rebuild the entire solution from scratch
Reinstall or update Xamarin.Forms
If you recently updated packages or are on an older version, try reinstalling:- Uninstall Xamarin.Forms from all projects in your solution
- Close your IDE completely
- Reopen and install the same stable version of Xamarin.Forms across all projects. If you were on an older version, you could also try updating to the latest stable release (just ensure all projects get the same update).
Validate your XAML files
Even though the error triggers inInitializeComponent(), invalid XAML (like missing namespaces, typos in type references) can indirectly cause this exception. Double-check yourApp.xamland any other XAML files for incorrect xmlns declarations or broken element references.
Relevant Code from Your Project
Error-throwing Constructor:
public App() { //Line throwing the error InitializeComponent(); MainPage = new NavigationPage(new Login.LogonFinal()); //Defines what page the app opens on when starting }
Full App.xaml.cs:
using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace NEA_I_MDL { public partial class App : Application { static Databases.AccountDatabaseController AccountDatabaseVar; public App() { //Line throwing the error InitializeComponent(); MainPage = new NavigationPage(new Login.LogonFinal()); //Defines what page the app opens on when starting } protected override void OnStart() { // Handle when your app starts } protected override void OnSleep() { // Handle when your app sleeps } protected override void OnResume() { // Handle when your app resumes } public static Databases.AccountDatabaseController AccountDatabaseFunc { get { if(AccountDatabaseVar == null) { AccountDatabaseVar = new Databases.AccountDatabaseController(); } return AccountDatabaseVar; } } } }
内容的提问来源于stack exchange,提问作者Galenth_Dysley

