As the title suggests I'm having trouble with running my .NET MAUI app on Windows all of a sudden. I am getting the following exception:
Exception thrown: 'System.TypeInitializationException' in forecAstIng.dll
An exception of type 'System.TypeInitializationException' occurred in forecAstIng.dll but was not handled in user code
The type initializer for '<Module>' threw an exception.
From the MauiProgram.CreateMauiApp() function in the automatically generated platform specific file App.xaml.cs that is created when starting a MAUI project:
using Microsoft.UI.Xaml;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace forecAstIng.WinUI
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
MauiProgram.cs:
using forecAstIng.View;
using Microsoft.Extensions.Logging;
using CommunityToolkit.Maui;
using forecAstIng.Services;
namespace forecAstIng
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.Services.AddSingleton<DataService>();
builder.Services.AddSingleton<ForecastsViewModel>();
builder.Services.AddSingleton<MainPage>();
builder.Services.AddTransient<MorePageViewModel>();
builder.Services.AddTransient<MorePage>();
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
}
csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<!-- Note for MacCatalyst:
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
<OutputType>Exe</OutputType>
<RootNamespace>forecAstIng</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Display name -->
<ApplicationTitle>forecAstIng</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>com.companyname.forecasting</ApplicationId>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" Color="#512BD4" BaseSize="32,32" />
<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
<ItemGroup>
<None Remove="resources\images\source.md" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.3" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<MauiXaml Update="View\SearchPrompt.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>
</Project>
I am on dotnet8 as I believe the build targets show as well.
I am not sure if it is relevant, but just before I started getting this exception I updated the nuget packages I use for my project, because I was having other issues with weird exceptions on windows inside of my program. I use these packages:
CommunityToolkit.Maui
CommunityToolkit.Mvvm
Microsoft.Extensions.Logging.Debug
Microsoft.Maui.Controls
Microsoft.Maui.Controls.Compatibility
Microsoft.NET.ILLink.Tasks
Newtonsoft.Json
Since the error occurs after updating the Nuget package, it occurs because of a package version conflict.
Exclude Newtonsoft.Json
, because it was last updated on 3/8/2023.
Exclude Microsoft.NET.ILLink.Tasks
, Microsoft.Extensions.Logging.Debug
, Microsoft.Maui.Controls
and Microsoft.Maui.Controls.Compatibility
, because they are automatically downloaded when creating a project. And Microsoft.NET.ILLink.Tasks cannot change the version.
So the problem is with CommunityToolkit.Maui
and CommunityToolkit.Mvvm
, both packages have recently released new versions.
The problem was eventually solved by downgrading the version of the CommunityToolkit.Mvvm
package from 8.3.0 to 8.2.2. I didn't currently find a issue on the Maui Github about version conflicts of the CommunityToolkit.Mvvm package, you can post this issue to Maui Github.