首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >.NET MAUI中的部分平台专用方法

.NET MAUI中的部分平台专用方法
EN

Stack Overflow用户
提问于 2022-10-10 05:45:14
回答 2查看 136关注 0票数 3

我试图在.NET MAUI中实现特定于plattform的部分方法,以获得数据库的连接字符串。

在“主要应用程序”中:

代码语言:javascript
运行
复制
namespace TestApp.DL;

public partial class BaseHandler
{
    public partial string GetDBPath();

    private string GetCnnPath() 
    {
        var dbPath = GetDBPath();
        var cnnPath = $"Data Source={dbPath}";

        return cnnPath;
    }

    ...
}

在项目中的平台文件夹中:

如果每一项都包含了具体的实现:

代码语言:javascript
运行
复制
namespace TestApp.DL;

// All the code in this file is only included on Android.
public partial class BaseHandler
{
    public string GetDBPath()
    {
        var dbName = "com.mycompany.mydatabase.db";
        return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
    }
}

...but I不断得到"Error CS8795: Partial‘BaseHandler.GetDBPath()“必须有一个实现部分,因为它有可访问性修饰符。(CS8795)。编译器似乎没有看到特定于平台的文件?注意,它们与主应用程序在一个单独的组装项目中,但我想这应该是可以的,因为fwk为我创建了文件夹?

EN

回答 2

Stack Overflow用户

发布于 2022-10-10 18:44:58

当您与部分类斗争时,您可以继续使用部分类,但避免使用部分方法。这一点在创建毛伊语库时尤其如此,如果这种方法倾向于失败,而在毛伊语应用程序中,编译效果很好。

“快速修复解决方案”,所有部分类显然必须使用相同的命名空间:

共享代码时,无论您使用什么,都需要将NET6_0更改为NET7_0

代码语言:javascript
运行
复制
public partial class BaseHandler
{
    private string GetCnnPath() 
    {
        var dbPath = GetDBPath();
        var cnnPath = $"Data Source={dbPath}";

        return cnnPath;
    }

#if (NET6_0 && !ANDROID && !IOS && !MACCATALYST && !WINDOWS && !TIZEN)

        public string GetDBPath()
        {
            throw new PlatformNotSupportedException();
        }
#endif
}

平台平台/Android中特定于平台的代码:

代码语言:javascript
运行
复制
public partial class BaseHandler
{
    public string GetDBPath()
    {
        var dbName = "com.mycompany.mydatabase.db";
        return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
    }
}
票数 3
EN

Stack Overflow用户

发布于 2022-10-10 08:37:23

首先,您的实现还必须使用分部关键字:

代码语言:javascript
运行
复制
namespace TestApp.DL;

// All the code in this file is only included on Android.
public partial class BaseHandler
{
    public partial string GetDBPath()
    {
        var dbName = "com.mycompany.mydatabase.db";
        return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
    }
}

然后,您应该确保您正在遵循多目标选择的指南:https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/configure-multi-targeting#configure-folder-based-multi-targeting

您需要用以下内容更新您的.csproj文件:

代码语言:javascript
运行
复制
<!-- Android -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-android')) != true">
  <Compile Remove="**\Android\**\*.cs" />
  <None Include="**\Android\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>

<!-- iOS -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-ios')) != true">
  <Compile Remove="**\iOS\**\*.cs" />
  <None Include="**\iOS\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>

<!-- Mac Catalyst -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-maccatalyst')) != true">
  <Compile Remove="**\MacCatalyst\**\*.cs" />
  <None Include="**\MacCatalyst\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>

<!-- Windows -->
<ItemGroup Condition="$(TargetFramework.Contains('-windows')) != true">
  <Compile Remove="**\Windows\**\*.cs" />
  <None Include="**\Windows\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>

否则,您将看到另一个编译器错误,因为您只能为任何部分方法声明提供一个主体。您需要为每个平台提供特定于平台的实现,并禁用那些不需要的实现的编译,或者您可以添加一个默认的实现,但是您需要基于文件的多目标,而不是基于平台的多目标(或两者的组合)。

我已经遇到了类似的问题,并在这里解决了它:MAUI: How to use partial classes for platform specific implementations together with net7.0 as TargetFramework in the SingleProject?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74010644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档