I’m having a problem compiling a cross platform MAUI library project implementing a partial class platform dependent. My project structure looks like this:
Project
Core
MyClass
Design
IMyInterface
Platform
Android
MyClass
Windows
MyClass
And my code looks like this:
ProjectDesignIMyInterface.cs
namespace Core
{
puclic interface IMyInterface
{
string SomeString { get; set; }
event EventHandler<int> OnSomethingHappend;
int SomeMethod();
}
}
ProjectCoreMyClass.cs
namespace Core
{
puclic partial class MyClass : IMyInterface
{
public event EventHandler<int> OnSomethingHappend;
}
}
ProjectPlatform*MyClass.cs
namespace Core
{
puclic partial class MyClass
{
string SomeString { get; set; }
int SomeMethod()
{
// Do some platform dependent stuff
}
}
}
As you can see I want my class to implement my interface, but have the relevant parts in the platform specific files in Platform*
. I made sure that they are placed in the same namespace. I also made sure to select a target platform for compilation.
At this point visual studio should build it using the partial class for the selected platform. However I get an error saying that MyClass
doesn’t implement my interface. If I copy the platform code into the Core
folder there is no problem. I guess it’s some kind of small setting that’s missing here, but I can’t find anything. For some reason it seems to ignore the platform folder when looking for the partial classes. All I can find is the general "partial class implementing interface" stuff. I didn’t find something related to a Maui SingleProject.
Edit: As asked, here is a minimal github repo. On my pc it creates the error.
2
Answers
I have found the solution, and what causes the error is actually very simple.
On the side of your RUN button you can choose what framework you want to compile to. In a normal MAUI app, you would choose e.g. android or windows here to decide what platform you are working/debugging on.
As it turns out, this is not all you need to keep in mind when working with partial classes in your platform folders, there are two parts to take into account.
First part is intellisense. It does not show errors based on what platform you select using the RUN button drop-down menu. Below your open file tabs are some drop-down menus showing the structure of the file. you can use them to move to a specific element (property, method, etc) of a class/interface etc implemented in that file. The left one lets you select the target framework that intellisense will use for marking code errors (see image).
I don't remember seeing the left one before MAUI, it might have been added specifically for single project. If you open e.g. a standard net6 library it will hace nothing to chose as there are no other targets. But I can't really say because I never paid any attention to them before.
The second part is the actual compilation. The answer to that is rather simple as well. Visual Studio dowsn't build the platform you select using the RUN button drop-down menu for building, only for running. It will build all platforms it seems.
You need a partial class with a default implementation (e.g.
throw new NotImplementedException();
for everything) for every platform.My project was new, and I had just started on the windows part. I expected it to compile only that part because I chose windows as target platform. But it wants to compile for all platforms. So you have to either add that default until you get to that platform or remove it from the project target frameworks until then.
On the core partial, inform the compiler that the missing members will be provided by a partial elsewhere: