Dumber_Texan2
Dumber_Texan2

Reputation: 978

The type or namespace name 'Platforms' does not exist in the namespace MyApp

I'm trying to implement platform-specific Dependency Injection in a .Net Maui app. The problem occurs in the MauiProgram.cs file.

builder.Services.AddTransient<IMyData, MyData>();

The problem is with MyData. It can't be resolved because it is in the Android folder and uses the MyApp.Platforms namespace. Platforms can't be resolved.

I realize that naming an app anything to do with "Android", as in MyAndroidApp, causes issues. I'm wondering if using "App", as in MyApp, in the name causes problems as well.

I am using a version of Visual Studio Enterprise 2022 that was downloaded and updated this week.

UPDATE 1

I created a new Maui app called MyName and I still can't get MyName.Platforms to resolve. Seems like a bug to me.

Upvotes: 1

Views: 2754

Answers (2)

Dumber_Texan2
Dumber_Texan2

Reputation: 978

I finally figured it out. When I read the entire error message, it states that it can't resolve the name in the iOS, Windows, MacCatalyst and Tizen projects. For me, it says nothing about Android. This makes sense. At Build time, it's looking for the file in all the Projects, but can't find it in some. It thinks you are building all the Projects. Here's how I tested my theory. If you create a Helper and add into all the Platforms (iOS, Windows, MacCatalyst & Tizen) you can Build your Solution. Here is what I did to test this out. I added a Static Class to all the Projects.

public static class StaticHelper
    {
        public static string GetName()
        {
            var test = "test";
            
            return test;
        }
    }

I was then able to Build my Project with this line of code in the MainPage.xaml.cs.

var service = StaticHelper.GetName();
            var test = service;

By the way, I tried excluding the Windows, MacCatalyst & Tizen Projects in another Solution and it caused major Build issues. Proceed with caution on that.

Upvotes: 2

Leandro Toloza
Leandro Toloza

Reputation: 2020

it's a bug or something specific to the framework. Also gerald on that video Gerarld in comments said: "something might’ve changed in this area causing it not to work anymore".

It happened to me and could solved it (At least in my case that was a class without dependency injection) following this github issue .

Also if you want to follow the track of the Platforms Specific Bugs you have this thread.

It's easy to reproduce your problem so you can report it on github if you want. Or wait to get solved. Let's remember that this project is still in constant development and problems arise all the time, I hope that the information will help you find a better solution.

Upvotes: 0

Related Questions