分享方式:


在適用於 Android 的 .NET 和適用於 iOS 應用程式的 .NET 中移 Xamarin.Essentials 轉程式代碼

Xamarin.Essentials 是幾乎每個 Xamarin 應用程式的基本連結庫,其功能現在是 .NET 多平臺應用程式 UI (.NET MAUI) 的一部分。

下列步驟概述在適用於 Android 的 .NET 或適用於 iOS 應用程式的 .NET 中使用原生裝置功能的程式 .NET MAUI:先前稱為 Xamarin.Essentials:

  1. 從適用於 Android 的 Xamarin.Essentials .NET 或適用於 iOS 應用程式的 .NET 中移除 NuGet 套件。
  2. $(UseMauiEssentials) 項目檔中的 build 屬性設定為 true 。 如需詳細資訊,請參閱 修改項目檔
  3. 呼叫 Platform.Init 方法,以初始化“essentials” 功能。 如需詳細資訊,請參閱 初始化平臺
  4. 視需要執行其他設定。 如需詳細資訊,請參閱 執行其他設定
  5. 新增必要功能的 using 指示詞。 如需詳細資訊,請參閱 新增using指示詞

重要

除了移除命名空間的參考Xamarin.Essentials以外,不需要在應用程式中使用Xamarin.Essentials.NET MAUI任何動作,因為 .NET MAUI 已經包含中的Xamarin.Essentials功能。

修改項目檔

.NET MAUI若要在適用於 Android 的 .NET 或適用於 iOS 應用程式的 .NET 中使用原生裝置功能,請修改您的項目檔,並將組建屬性設定$(UseMauiEssentials)true

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-android</TargetFramework>
    ...
    <UseMauiEssentials>true</UseMauiEssentials>
  </PropertyGroup>
</Project>

初始化平臺

在所啟動的任何Activity專案中,您必須從 OnCreate 方法呼叫 Platform.Init 命名空間中的 Microsoft.Maui.ApplicationModel 方法:

using Android.Content.PM;
using Android.Runtime;
using Microsoft.Maui.ApplicationModel;

namespace MyAndroidApp;

[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
    protected override async void OnCreate(Bundle? savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Platform.Init(this, savedInstanceState);
        // ...
    }
}

方法 Platform.Init 需要自 Application 變數或 Activity 自變數和 Bundle 自變數。

執行其他設定

靜態 Platform 類別包含平臺特定的協助程式。

member 目的
ActivityStateChanged 當任何活動的狀態變更時所引發的事件。
AppContext 屬性,取得 Context 表示目前應用程式內容的物件。
CurrentActivity 屬性,取得表示目前活動之目前 Activity 物件。
Intent 包含字串的 ActionAppAction 靜態類別,這是應用程式動作所使用的識別碼 Intent
OnNewIntent Intent在叫用應用程式動作時,從活動的覆寫方法傳遞 。
OnResume Activity當在叫用應用程式動作時,Activity從活動的覆寫方法傳遞 。
OnRequestPermissionsResult 傳遞活動覆寫方法所產生的許可權要求,以處理內部許可權要求。
WaitForActivityAsync 等候 Activity 建立或變成作用中。

若要存取目前 ContextActivity 執行中的應用程式:

var context = Platform.AppContext;

// Current Activity or null if not initialized or not started.
var activity = Platform.CurrentActivity;

Activity如果有需要 的情況,但應用程式尚未完全啟動,請呼叫 WaitForActivityAsync 方法:

var activity = await Platform.WaitForActivityAsync();

若要處理運行時間許可權要求,請覆寫 OnRequestPermissionsResult 每個 Activity 中的方法,並從中呼叫 Platform.OnRequestPermissionsResult 方法:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

除了取得目前的 Activity之外,您也可以註冊生命週期事件:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    Platform.Init(this, bundle);
    Platform.ActivityStateChanged += Platform_ActivityStateChanged;
}

protected override void OnDestroy()
{
    base.OnDestroy();
    Platform.ActivityStateChanged -= Platform_ActivityStateChanged;
}

void Platform_ActivityStateChanged(object sender, ActivityStateChangedEventArgs e) =>
    Toast.MakeText(this, e.State.ToString(), ToastLength.Short).Show();

活動狀態為:

  • 建立時間
  • 已繼續
  • 已暫停
  • 終結
  • SaveInstanceState
  • 已開始
  • 已停止

新增 using 指示詞

適用於 iOS 的 .NET 和適用於 Android 的 .NET 隱含 global using 指示詞不包含原生裝置功能的命名空間 .NET MAUI。 因此, using 命名空間的 Xamarin.Essentials 指示詞應該取代為 using 包含必要功能的命名空間指示詞:

Namespace 目的
Microsoft.Maui.ApplicationModel 應用程式模型功能,包括應用程式動作、許可權和版本追蹤。
Microsoft.Maui.ApplicationModel.Communication 通訊功能,包括聯繫人、電子郵件和網路功能。
Microsoft.Maui.Devices 裝置功能,包括電池、感測器、手電筒筒和觸覺回饋。
Microsoft.Maui.Media 媒體功能,包括媒體選擇和文字到語音轉換。
Microsoft.Maui.ApplicationModel.DataTransfer 共用功能,包括剪貼簿和檔案共用。
Microsoft.Maui.Storage 儲存體 功能,包括檔案挑選和安全記憶體。

如需每個命名空間中功能的詳細資訊,請參閱 平臺整合