Xamarin到MAUI的遷移、頁面導航與數據綁定、平台特定功能調用

一、Xamarin到MAUI的遷移:從多項目到單項目的演進

Xamarin.Forms到.NET MAUI的遷移是跨平台開發的一次關鍵升級,核心在於單項目系統API統一化

  1. 項目結構重構
  • Xamarin.Forms:採用多項目結構,需分別維護iOS、Android、Windows等平台的代碼庫,導致重複配置和資源管理複雜。
  • .NET MAUI:通過單項目系統合併所有平台代碼,資源集中存放在Resources文件夾(如圖片、字體),並通過MSBuild任務自動生成資源ID。例如:
xml
 <ItemGroup>
 
     <MauiImage Include="Resources\Images\icon.png" />
 
     <MauiFont Include="Resources\Fonts\OpenSans.ttf" />
 
 </ItemGroup>
  • 遷移工具:使用try-convert工具自動轉換項目結構,或手動調整命名空間(如Xamarin.FormsMicrosoft.Maui)。
  1. API與依賴注入升級
  • 命名空間變更:核心API從Xamarin.Forms遷移至Microsoft.Maui,需全局替換命名空間(如Xamarin.Forms.ApplicationMicrosoft.Maui.Controls.Application)。
  • 依賴服務:Xamarin.Forms依賴DependencyService,而MAUI通過通用主機(MauiApp.CreateBuilder())配置服務,例如:
csharp
 var builder = MauiApp.CreateBuilder();
 
 builder.Services.AddScoped<ILocalService, LocalService>();
  • 資源管理:MAUI引入集中式資源管理,避免Xamarin.Forms中多平台資源重複定義的問題。
  1. 性能與啓動優化
  • MAUI採用類似ASP.NET Core的啓動配置,支持自定義服務註冊和字體加載,例如:
csharp
 builder.ConfigureFonts(fonts => {
 
     fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
 
 });
  • 移除Xamarin.Forms中冗餘的初始化代碼(如Forms.Init()),改用MauiApp.CreateBuilder()統一管理。

二、頁面導航與數據綁定:MVVM模式的深度集成

MAUI通過路由註冊數據綁定強化了頁面交互能力,支持MVVM模式的無縫集成。

  1. 路由導航
  • AppShell.xaml.cs中註冊路由,將字符串路徑與實際頁面類關聯:
csharp
 Routing.RegisterRoute("buddiespage", typeof(BuddiesPage));
 
 Routing.RegisterRoute("buddydetailspage", typeof(BuddyDetailsPage));
  • 通過Shell.Current.GoToAsync()實現頁面跳轉,例如:
csharp
 await Shell.Current.GoToAsync("//buddydetailspage");
  1. 數據綁定與命令
  • 屬性綁定:ViewModel需實現INotifyPropertyChanged接口,確保UI與數據同步:
csharp
 public class MainViewModel : INotifyPropertyChanged {
 
     private string _message;
 
     public string Message {
 
         get => _message;
 
         set {
 
             _message = value;
 
             OnPropertyChanged(nameof(Message));
 
         }
 
     }
 
     public event PropertyChangedEventHandler PropertyChanged;
 
     protected void OnPropertyChanged(string propertyName) => 
 
         PropertyChanged?.Invoke(this, new(propertyName));
 
 }
  • XAML綁定:在頁面中綁定ViewModel屬性:
xml
 <ContentPage.BindingContext>
 
     <local:MainViewModel />
 
 </ContentPage.BindingContext>
 
 <Label Text="{Binding Message}" />
  • 命令綁定:通過ICommand實現按鈕點擊等交互,例如:
csharp
 public ICommand AddCommand => new Command(() => {
 
     // 添加邏輯
 
 });
xml
 <Button Text="Add" Command="{Binding AddCommand}" />

三、平台特定功能調用:跨平台兼容與擴展

MAUI通過平台擴展機制PlatformConfiguration)支持對iOS、Android等平台的差異化定製。

  1. Android平台特定功能
  • 軟鍵盤模式:控制鍵盤彈出時的窗口調整方式:
csharp
 Application.Current.On<Android>().SetWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
  • TabbedPage工具欄位置:支持頂部或底部佈局:
csharp
 tabbedPage.On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
  • SwipeView滑動模式:配置滑動過渡效果:
csharp
 swipeView.On<Android>().SetSwipeTransitionMode(SwipeTransitionMode.Reveal);
  1. iOS平台特定功能
  • 導航欄半透明效果
csharp
 Application.Current.On<iOS>().SetNavBarIsTranslucent(true);
  • ListView分隔符樣式
csharp
 listView.On<iOS>().SetSeparatorStyle(SeparatorStyle.None);
  • 日期選擇器交互:控制DatePicker的項選擇時間:
csharp
 datePicker.On<iOS>().SetUpdateMode(UpdateMode.WhenFocused);
  1. 跨平台代碼編寫
  • 使用#if預處理器指令區分平台代碼:
csharp
 #if ANDROID
 
     // Android特定邏輯
 
 #elif IOS
 
     // iOS特定邏輯
 
 #endif
  • 通過PlatformConfiguration的鏈式調用簡化平台配置,例如:
csharp
 element.On<Android>().SetSomeProperty(value).On<iOS>().SetAnotherProperty(value);