# 開發.Net7 Maui(iOS和Windows)所遇到的問題與解決 ## SQLite context報錯 1. 錯誤訊息: ``` C# System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: e_sqlite3 ``` 2. 原因: Microsoft.EntityFrameworkCore.Sqlite ==6.0.11版==預設安裝的SQLitePCLRaw.bundle_e_sqlite.2.0.6版有問題 3. 參考文件: https://blog.darkthread.net/blog/sqlite-n-netfx/ 4. 解決: 使用Microsoft.EntityFrameworkCore.Sqlite ==6.0.13版== ## Net 7無法Hot Restart 1. 在MauiProgram的CreateMauiApp最一開始加上這段即可解決 ``` C# AppContext.SetSwitch("System.Reflection.NullabilityInfoContext.IsSupported", true); ``` 2. 參考資料: https://github.com/xamarin/xamarin-macios/issues/16228#issuecomment-1400850817 ## iOS無法改Splash頁面和App icon 1. 參考資料: https://github.com/dotnet/maui/issues/8685#issuecomment-1286038152 2. 解決: 將參考資料中的Code貼到專案檔當中,並且不可使用Debug(Hot Restart),要==使用Release發佈ipa檔裝到實體裝置上== ``` C# <Target Name="_FixMauiSplashScreens" BeforeTargets="ResizetizeCollectItems" Condition="'$(_ResizetizerIsiOSApp)' == 'True'"> <!-- add a hash to each file --> <GetFileHash Files="@(MauiSplashScreen)" HashEncoding="base64"> <Output TaskParameter="Items" ItemName="_MauiSplashScreensWithHashes" /> </GetFileHash> <!-- update the inputs files --> <WriteLinesToFile File="$(_ResizetizerInputsFile)" Lines="@(MauiImage->'File=%(Identity);Link=%(Link);BaseSize=%(BaseSize);Resize=%(Resize);TintColor=%(TintColor);Color=%(Color);IsAppIcon=%(IsAppIcon);ForegroundScale=%(ForegroundScale);ForegroundFile=%(ForegroundFile)')" Overwrite="true" WriteOnlyWhenDifferent="true" /> <WriteLinesToFile File="$(_MauiSplashInputsFile)" Lines="@(_MauiSplashScreensWithHashes->'File=%(Identity);Link=%(Link);BaseSize=%(BaseSize);Resize=%(Resize);TintColor=%(TintColor);Color=%(Color);ForegroundScale=%(ForegroundScale);FileHash=%(FileHash)')" Overwrite="true" WriteOnlyWhenDifferent="true" /> <!-- get the hash of the inputs file --> <GetFileHash Files="$(_MauiSplashInputsFile)" HashEncoding="base64"> <Output TaskParameter="Hash" PropertyName="_MauiSplashInputsFileHash" /> </GetFileHash> <!-- update the splahs items and set the filename to be the 'splash_<hash>' --> <ItemGroup> <MauiSplashScreen Update="@(MauiSplashScreen)" Link="splash_$([MSBuild]::ValueOrDefault('$(_MauiSplashInputsFileHash)', '').Replace('+','').Replace('/','').Replace('=',''))" /> </ItemGroup> </Target> ``` 3. 注意: 發佈前可先將專案==重建==,重置專案檔設定 4. 單純更換Splash page和App icon可參考這篇官方文件: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/images/app-icons?view=net-maui-7.0&tabs=windows#platform-specific-configuration ## Windows和Ios 裝置id 1. iOS: - 程式: ``` C# DeviceId = UIDevice.CurrentDevice.IdentifierForVendor?.AsString(); ``` - 參考文件: https://medium.com/appworks-school/device-check-35556ab74d(標題:IDFV — Identifier For Vendor) 2. Windows: - 程式: ``` C# var deviceInformation = new EasClientDeviceInformation(); DeviceId = deviceInformation.Id.ToString(); ## Windows平板 顯示遠端來源照片 1. Windows平板沒辦法直接讀取來源為Http/Https的圖片(iOS可以) -> 要先把圖片來源轉成ByteArray再讓平板讀取 2. 程式(url為圖片來源): ``` C# using (HttpClient myHttpClient = new HttpClient()) { var byteArray = await myHttpClient.GetByteArrayAsync(url); source = $"data:image/jpg;base64, {Convert.ToBase64String(byteArray)}"; return source; } ``` ## Ios拍照後照片自動轉向 1. Maui 的iOS在拍照的時候,會有把拍完的照片轉90度的問題。目前無解,除非官方把套件Bug修好 -> 目前只能將拍好的照片倒轉90度回來,讓使用者看到正常的照片 2. 程式: ``` C# using Microsoft.Maui.Graphics.Platform; using IImage = Microsoft.Maui.Graphics.IImage; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Formats.Jpeg; //利用Maui原生的套件功能將拍完的原圖縮小(較不占記憶體) IImage newImage = image.Resize(500, 400, Microsoft.Maui.Graphics.ResizeMode.Stretch, true); var imageBytes = newImage.AsBytes(); //利用ImageSharp套件將原圖轉90度 using (SixLabors.ImageSharp.Image image2 = SixLabors.ImageSharp.Image.Load(imageBytes)) { image2.Mutate(x => x.Rotate(90)); using FileStream localFileStream = File.OpenWrite(PhotoPath); await image2.SaveAsync(localFileStream, new JpegEncoder()).ConfigureAwait(false); } ``` 因為Maui Graphic套件沒有讓照片轉向的功能,所以另外找可讓照片轉向的套件: ==ImageSharp==