Skip to content
Rain Hu's Workspace
Go back

[IT] .NET Maui

Rain Hu

.NET Maui


架構

+ 一個 .NET Maui 專案底下,預設會有幾個資料夾與檔案,其關係如下圖: + `/Platforms` 底下的各個資料夾為不同平台的入口,不同的平台各有一個 `Program.cs`。 + 各個 `Program.cs` 內又會透過注入該 namespace 底下的 `AppDelegate` ,將入口指向 `MauiProgram` 的 `CreateMauiApp()`,就此將不同平台路由到 `MauiProgram.cs` 這個統一的入口。 ```Csharp public class Program { static void Main(string[] args) { UIApplication.Main(args, null, typeof(AppDelegate)); } }

[Register(“AppDelegate”)] public class AppDelegate : MauiUIApplicationDelegate { protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); }

+ 關係如下圖:
```mermaid
graph TD;
  iOS-->MauiProgram.cs;
  Android-->MauiProgram.cs;
  Windows-->MauiProgram.cs;
  macOS-->MauiProgram.cs;
  MauiProgram.cs-->App;
  App-->AppShell;
  AppShell-.->Page1;
  AppShell-.->Page2;
  AppShell-.->Page3;
  AppShell-.->Page4;
public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });
        return builder.Build();
    }
}
public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new AppShell();
    }
}

Router

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();

        Routing.RegisterRoute(nameof(Page1), typeof(Page1));
        Routing.RegisterRoute(nameof(Page2), typeof(Page2));
        Routing.RegisterRoute(nameof(Page3), typeof(Page3));
    }
}

xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="MoneyTrack.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MoneyTrack"
    xmlns:views="clr-namespace:MoneyTrack.Views"
    Shell.FlyoutBehavior="Disabled"
    Title="MoneyTrack">

    <ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate views:MoneyTackPage}"
        Route="MoneyTackPage" />

</Shell>

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MoneyTrack.Views.ContactsPage"
             Title="Contacts">
    <VerticalStackLayout Spacing="5">
        <Label 
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
        <Button x:Name="btn1" Clicked="btn1_Clicked" Text="click1"></Button>
        <Button x:Name="btn2" Clicked="btn2_Clicked" Text="click2"></Button>
    </VerticalStackLayout>
</ContentPage>

Shell

public void btnPage1_Clicked()
{
    Shell.Current.GoToAsync(nameof(Page1));
}

GoToAsync

void btnPage1_Clicked(object sender, EvertArgs e)
{
    Shell.Current.GoToAsync($"{nameof(Page1)}");  // 前往 Page1
}

void btnCancel_Clicked(object sender, EvertArgs e)
{
    Shell.Current.GoToAsync($"//{nameof(MainPage)}");  // 回到 MainPage
}

void btnCancel_Clicked(object sender, EvertArgs e)
{
    Shell.Current.GoToAsync($"..");  // 回到上一頁
}

QueryProperty

[QueryProperty(nameof(PageId), "Id")]
public partial class Page1 : ContentPage
{
	public EditContactPage()
	{
		InitializeComponent();
	}
    public string ContactId
    {
        set
        {
            lblName.Text = Id;
        }
    }
}

Share this post on:

Previous
[ML] 選擇 loss function/ optimizer/ metrics
Next
[Hugo] 使用 Hugo-notice