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:
Project\Design\IMyInterface.cs
namespace Core
{
puclic interface IMyInterface
{
string SomeString { get; set; }
event EventHandler<int> OnSomethingHappend;
int SomeMethod();
}
}
Project\Core\MyClass.cs
namespace Core
{
puclic partial class MyClass : IMyInterface
{
public event EventHandler<int> OnSomethingHappend;
}
}
Project\Platform\*\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.
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.