You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Xamarin.Forms中App.xaml.cs的System.TypeLoadException异常?

System.TypeLoadException for Xamarin.Forms.Xaml.Diagnostics.VisualDiagnostics in App.xaml.cs

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:

    1. Right-click your solution and select "Clean Solution"
    2. Manually delete the bin and obj folders from your shared, Android, and iOS projects
    3. Rebuild the entire solution from scratch
  • Reinstall or update Xamarin.Forms
    If you recently updated packages or are on an older version, try reinstalling:

    1. Uninstall Xamarin.Forms from all projects in your solution
    2. Close your IDE completely
    3. 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 in InitializeComponent(), invalid XAML (like missing namespaces, typos in type references) can indirectly cause this exception. Double-check your App.xaml and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:47:38