2

In the MAUI class library, within the platform-specific code, I have the following class:

public partial class Connector
{
    internal partial void ReadData(string command, Action<string, string> readCallback)
    {

    }
    internal partial void WriteData(string command, Action<string, string> readCallback);    
}

Outside platforms folder I have the following class:

public partial class Connector
  {
      internal partial void ReadData(string command, Action<string, string> readCallback);

      internal partial void WriteData(string command, Action<string, string> readCallback)
      {

      }
  }

The provided code functions correctly when implemented in a MAUI app. However, when incorporated into a MAUI library, an issue arises: the compiler reports an error stating that the partial method "ReadData" must have an implementation, and there is no definition found for the "WriteData" method. WHY?

error:

Error CS0759 No defining declaration found for implementing declaration of partial method 'Connector.WriteData(string, Action<string, string>)'.

Error CS8795 Partial method 'Connector.ReadData(string, Action<string, string>)' must have an implementation part because it has accessibility modifiers.

Some articles recommend against employing partial methods in cross-platform libraries without mentioning the reason. Refer to the following resources for more information:

https://learn.microsoft.com/en-us/answers/questions/992380/partial-classes-and-methods-dont-support-c-9-featu

Maui class library: Partial Method issue

Before you call it an insufficient information, I recommend you create a MAUI library/App and simply copy-paste my code. It should not take a maximum of 5 minutes.

11
  • Please show the errors you see and show where you've defined those different parts of the class. Since you've marked the methods as internal, they're not visible outside of the assembly they're defined in: learn.microsoft.com/dotnet/csharp/language-reference/keywords/… Also, the different parts of the class cannot be in different assemblies, AFAIK. Commented Jan 20, 2024 at 13:20
  • @Julian, the identical code, without altering a single space, functions properly in the MAUI app. However, it fails to work even when I declare the methods above as public. And they are not different assembly. Commented Jan 20, 2024 at 13:25
  • That's not sufficient information. Please show the structure of the library project. Commented Jan 20, 2024 at 13:28
  • @Julian, my library doesn't contain anything else. I recommend you create a MAUI library and simply copy-paste my code. It should take a maximum of 2 minutes Commented Jan 20, 2024 at 13:30
  • @Julian, Also the MAUI app I'm referring to doesn't include anything else besides the code I've shared. MAUI app is compiling but MAUI lib doesn't. Commented Jan 20, 2024 at 13:34

1 Answer 1

2

I can reproduce your problem. It was caused by the different namespace for the partial class. The official document about Implementing the API per platform said:

Platform implementations must be in the same namespace and same class that the cross-platform API was defined in.

But when you create a class in the Platforms folder, its default namespace is YourProjectName.Platforms.PlatformName. Please make the partial classes's namespace in the platforms folder is same as it in the share project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.