Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The target platform must be set to Windows" when using Prism.DryIoc in .NET 6.0 Class Library

I am using a class library for tests and I need to configure DryIoc, so I reference the Nuget package Prism.DryIoc. Note that I am working on a Xamarin solution and do not have (or need) any WPF (or other Windows) components.

When my class library is set to .NET Core 3.1, it compiles correctly. If I change it to .NET 5.0 or .NET 6.0 however, I get this error:

Error NETSDK1136 The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so. C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets 250

When I review the references from Prism.DryIoc I see that it references Prism.Wpf.

Does anyone know a way to complete remove any Wpf dependencies, so that I can compile my library directly with .NET 6.0 without issues and without having to specify Windows as target framework?

Steps to reproduce:

  1. Open Visual Studio 2022
  2. Create a new project of type Class Library targeting .NET 6.0
  3. Add Nuget reference to the newest Prism.DryIoc (8.1.97 at the moment)
  4. Rebuild (sometimes a second rebuild it needed to see the error)

Sample class library attached.

like image 366
Vladimir Avatar asked Sep 06 '25 02:09

Vladimir


2 Answers

For anyone coming here in the future: The correct package to use with any Xamarin application is Prism.DryIoc.Forms (and not Prism.DryIoc) as described here.

My project was using Prism.DryIoc in order to create a DryIoc bootstrapper inheriting from PrismBootstrapperBase, which is in the Wpf project. I solved the problem by extracting the needed code from PrismBootstrapperBase and removing the reference to Prism.DryIoc. Here is the bootstrapper's code for reference:

public class TestsBootstrapper
{
    #region Properties
    IContainerExtension _containerExtension;
    public IContainerProvider Container => _containerExtension;
    #endregion

    #region Run
    public void Run()
    {
        ContainerLocator.SetContainerExtension(CreateContainerExtension);
        this._containerExtension = ContainerLocator.Current;
        RegisterTypes(this._containerExtension);
        this._containerExtension.FinalizeExtension();
    }
    
    private IContainerExtension CreateContainerExtension()
    {
        return new DryIocContainerExtension(new Container(DryIocContainerExtension.DefaultRules));
    }
    #endregion

    #region RegisterTypes
    private void RegisterTypes(IContainerRegistry container)
    {
        // Add required registratoins here
    }
    #endregion
}
like image 69
Vladimir Avatar answered Sep 09 '25 00:09

Vladimir


Xamarin Forms uses .Net Standard 2.0 or 2.1 libraries. It is not compatible with .Net Core or .Net 5-6 projects, nor will it be until it is Migrated to Maui in 2022. At which point it will use .Net 5 as standard.

like image 22
Mark Barton Avatar answered Sep 09 '25 00:09

Mark Barton