4

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.

Vladimir
  • 1,425
  • 16
  • 31
  • What is your goal? Is there some requirement for .net 5 or 6, or is this simply a desire to be on the latest .net? Even if you find a solution, you won't be able to reference a .net5+ library in an X-Forms project. NOTE: a .NET Standard 2.0 Xamarin Forms app runs fine on .net5 or 6 runtime - the latest .net's are backwards compatible - so the only reason to go to .net6 is if there is some new API you need. In which case, you'll have to wait for MAUI's release. – ToolmakerSteve Dec 15 '21 at 17:50
  • 1
    The main code is in a .Net Standard 2.1 library. The problem I have here is in a test library and test libraries must have a runtime, i.e. either .net framework 4.x, .net core or .net 5/6. They don't have any code that runs on Xamarin itself but use the same DryIoc registrations. In order to run tests e.g. in Azure Devops, they may not use Windows components of any kind. – Vladimir Dec 15 '21 at 18:03

2 Answers2

4

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
}
Vladimir
  • 1,425
  • 16
  • 31
3

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.

Mark Barton
  • 847
  • 6
  • 15
  • The main code is in a .Net Standard 2.1 library. The problem I have here is in a test library and test libraries must have a runtime, i.e. either .net framework 4.x, .net core or .net 5/6. They don't have any code that runs on Xamarin itself but use the same DryIoc registrations. In order to run tests e.g. in Azure Devops, they may not use Windows components of any kind. – Vladimir Dec 15 '21 at 18:03
  • The Xamarin team decided that it's not necessary to port Xamarin.Forms 5 to .net6 https://github.com/xamarin/Xamarin.Forms/issues/14829 – juwens Jan 03 '22 at 17:36
  • But i think your explanation is not technically correct. Net6.0 is a superset of netstandard 2.1, so the code should be runnable. I think the problem is in the build scripts of xamarin or xamarin.forms. – juwens Jan 03 '22 at 17:43