admin管理员组

文章数量:1279187

I have problems to implement any platform dependent classes in a MAUI project. The compiler produces errors if I use a method which has a return value.

I implemented the function first for each platform (eg. Windows):

namespace MauiApp.Platforms
{
    public partial class Memory
    {
        private string name = "";

        partial void init()
        {
            name = "Windows";
        }

        public partial string getName()
        {
            return name;
        }
    }
}

Then I implement it in the core as follows:

namespace MauiApp.Platforms
{
    public partial class Memory
    {
        partial void init();
    
        partial string getName();
    }
}

With the method 'init' there is no problem. It works and can be compiled.

But with the 'getName' I get the error that the partial method getName must have an accessibility modifier because it has a non-void return type as well as a missing member declaration error on each platform. But if I set the public modifier in the core method to fix this, the errors on each platform disappear, but I get the error, that I must have an implementation part because it has accessibility modifiers. So If I do any implementation in the core like:

public partial string getName() { return ""; }

I get more errors, that a partial method may not have multiple implementing declarations and no defining declaration found for implementing declaration on each platform.

Generally there seems to be a problem using access modifiers when implementing platform dependent methods. Access modifiers work on each platform but there is a problem in the shared method on the core. If no access modifier is used, compiler errors appear on each platform and if an access modifier is used, the complier requests any implementation, but this does not work too.

How can I implement platform dependent methods using return values and an access modifier as well in MAUI ?

I have problems to implement any platform dependent classes in a MAUI project. The compiler produces errors if I use a method which has a return value.

I implemented the function first for each platform (eg. Windows):

namespace MauiApp.Platforms
{
    public partial class Memory
    {
        private string name = "";

        partial void init()
        {
            name = "Windows";
        }

        public partial string getName()
        {
            return name;
        }
    }
}

Then I implement it in the core as follows:

namespace MauiApp.Platforms
{
    public partial class Memory
    {
        partial void init();
    
        partial string getName();
    }
}

With the method 'init' there is no problem. It works and can be compiled.

But with the 'getName' I get the error that the partial method getName must have an accessibility modifier because it has a non-void return type as well as a missing member declaration error on each platform. But if I set the public modifier in the core method to fix this, the errors on each platform disappear, but I get the error, that I must have an implementation part because it has accessibility modifiers. So If I do any implementation in the core like:

public partial string getName() { return ""; }

I get more errors, that a partial method may not have multiple implementing declarations and no defining declaration found for implementing declaration on each platform.

Generally there seems to be a problem using access modifiers when implementing platform dependent methods. Access modifiers work on each platform but there is a problem in the shared method on the core. If no access modifier is used, compiler errors appear on each platform and if an access modifier is used, the complier requests any implementation, but this does not work too.

How can I implement platform dependent methods using return values and an access modifier as well in MAUI ?

Share Improve this question asked Feb 24 at 8:53 TomTom 1097 bronze badges 2
  • For a partial void method, you can just stick the method bodies together. Which works fine so long as the execution order doesn't matter. But a method can only return once, so clearly there's no way for the compiler to glue multiple method bodies with return statements together. – Jeremy Lakeman Commented Feb 25 at 5:04
  • Don't do any implementation(such as return "") in the cross-platform partial class and it will work. – Liqun Shen-MSFT Commented Feb 27 at 5:59
Add a comment  | 

1 Answer 1

Reset to default 0

I recommend you refer to the official doc to use partial class: Partial classes. To sum up, you have to define two classes, the cross-platform partial class and the platform-specific partial class,

The cross-platform partial class typically defines members, but doesn't implement them, and is built for all platforms. This class shouldn't be placed in any of the Platforms child folders of your project, because doing so would make it a platform-specific class.

The platform-specific partial class typically implements the members defined in the cross-platform partial class, and is built for a single platform. This class should be placed in the child folder of the Platforms folder for your chosen platform.

So, this is your platform-specific partial class, you should put this file in the folder of Platforms > Windows folder

namespace MauiAppTest
{

    public partial class Memory
    {
        private string name = "";

        partial void init()
        {
            name = "Windows";
        }

        public partial string getName()
        {
            return name;
        }
    }
}

And this is your cross-platform partial class, and don't put cross-platform partial class in any of the Platforms child folders. Please keep the two files in the same namespace.

namespace MauiAppTest
{
    public partial class Memory
    {
        partial void init();

        //don't return "" for getName method,
        public partial string getName();
    }
}

Then you can easily invoke this method anywhere,

var va = new Memory().getName();

Hope it helps.

本文标签: cPlatform dependent implementations with return values do not work in MAUIStack Overflow