1. programming 1.1 C# 1.5 MAUI

(114) Save Files With Just 1 Line of Code with .NET MAUI FileSaver! – YouTube(目前測試原生MAUI才可以使用,Hybrid還不知如何解)

2024 年 2 月 8 日

可能可以用來匯出檔案,不然使用者好像無法直接看到MAUI執行的資料夾內容。

2024-02-09

第一次測試 – 失敗

他馬的試了老半天,就是不知道 MainPage(IFileSaver filesaver)到底從哪邊傳進來的。本來想說透過新增一個 MAUI Native 頁面來專門放這個功能,結果也是遇到一樣的問題。相關程式碼如下:

MauiProgram.cs

using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Storage;
using MauiApp5_Reminder_Json.Components.Pages;
using Microsoft.Extensions.Logging;

namespace MauiApp5_Reminder_Json
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                // Initialize the .NET MAUI Community Toolkit by adding the below line of code
                .UseMauiCommunityToolkit()
                // After initializing the .NET MAUI Community Toolkit, optionally add additional fonts
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
            builder.Services.AddSingleton<IFileSaver>(FileSaver.Default);
            builder.Services.AddTransient<Export>();

            builder.Services.AddMauiBlazorWebView();

#if DEBUG
            builder.Services.AddBlazorWebViewDeveloperTools();
            builder.Logging.AddDebug();
#endif

            return builder.Build();
        }
    }
}

Export.xaml.cs

using CommunityToolkit.Maui.Storage;

namespace MauiApp5_Reminder_Json.Components.Pages;

public partial class Export : ContentPage
{
    IFileSaver filesaver;
    CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    public Export(IFileSaver filesaver)
	{
		InitializeComponent();
        this.filesaver = filesaver;
	}
}

第二次測試 – 成功(Windows/Android)

這次改用原生maui來試試。

馬的又遇到一堆雷,專案名稱含底線居然會出錯!太長也會出錯!

終於成功了,感動

Windows:

Android:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *