slash shogdhe
slash shogdhe

Reputation: 4187

Maui class library: Partial Method issue

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.

Upvotes: 2

Views: 707

Answers (1)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14244

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.

Upvotes: 1

Related Questions