簡體   English   中英

.NET MAUI 中的部分平台特定方法

[英]Partial platform specific methods in .NET MAUI

我正在嘗試在 .NET MAUI 中實現平台特定的部分方法來獲取數據庫的連接字符串。

在“主應用程序”中:

namespace TestApp.DL;

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

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

        return cnnPath;
    }

    ...
}

在項目的平台文件夾中:

在此處輸入圖像描述

其中每個都包含平台特定的實現:

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;
    }
}

...但我不斷收到“錯誤 CS8795:分部方法‘BaseHandler.GetDBPath()’必須具有實現部分,因為它具有可訪問性修飾符。(CS8795)”。 編譯器似乎看不到特定於平台的文件? 請注意,它們與主應用程序位於一個單獨的程序集項目中,但我想這應該沒問題,因為 fwk 為我創建了文件夾?

當您與分部斗爭時,您可以繼續使用分部類,但避免使用分部方法。 在創建 maui 庫時尤其如此,如果這種方法容易出錯,而在 maui 應用程序中編譯工作正常。

“快速修復解決方案”,所有分部類顯然必須使用相同的命名空間:

共享代碼,無論您使用什么,您都希望將NET6_0更改為NET7_0

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
}

平台 Platforms/Android 中您的平台特定代碼:

public partial class BaseHandler
{
    public string GetDBPath()
    {
        var dbName = "com.mycompany.mydatabase.db";
        return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
    }
}

首先,您的實現也必須使用 partial 關鍵字:

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/do.net/maui/platform-integration/configure-multi-targeting#configure-基於文件夾的多目標

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

<!-- 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 with.net7.0 as TargetFramework in the SingleProject?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM