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 |1 Answer
Reset to default 0I 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
版权声明:本文标题:c# - Platform dependent implementations with return values do not work in MAUI - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741284545a2370193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
return ""
) in the cross-platform partial class and it will work. – Liqun Shen-MSFT Commented Feb 27 at 5:59