Open51

Xamarin.Forms から MAUI への移行するときに踏んだ罠集

ProudustProudust

Xamarin.Forms.Visual.Material の代替が無い。
これは Xamarin.Forms.Visual.Material が依存していた Material Components for iOS がメンテナンスモードに移行したため。
Android 版くらいはあってもよい気もするが、Backlog 行きにされているため望み薄。
初期の Style をすべて剝がしたら Android のバージョンに対応した Material Design になっているような気がする。

https://github.com/dotnet/maui/issues/9689

Hidden comment
ProudustProudust

Application 属性がどうのこうのでビルド失敗。
MauiApplication を継承したクラスとは別に [assembly: Application] 属性が付いているから。
[assembly: Application] を削除すれば解決。

ProudustProudust

6.0.547 (SR7) 現在、Android で Frame や BoxView の透明度が反映されない。
https://github.com/dotnet/maui/issues/10503

ProudustProudust

再現しなくなったので直ったのか、それとも条件が違ってしまっているのか、いずれにせよ要再調査

ProudustProudust

VS2022 17.4.2 現在、以下のような XAML を書くと Environment は MAUI プロジェクトでサポートされていません。 というような警告が出る。
ビルドは通るし実行も問題なくできるのでおそらく VS のバグ。

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="A" />
            <Span Text="{Static x:Environment.NewLine}" />
            <Span Text="B" />
        </FormattedString>
    </Label.FormattedText>
</Label>
ProudustProudust

この例の場合 &#xA; で改行しろと言われると、それはそうとしか言えない。

ProudustProudust

6.0.547 (SR7) 現在、XAML の ResourceDictionary に同じ TargetType が指定された暗黙のスタイルが複数存在すると、実行時に無言でクラッシュする?要調査&バグ報告。

ProudustProudust

6.0.547 (SR7) 現在、Android で CollectionView が信じられないくらい遅い。2桁件数でも平気で数秒プチフリーズする。
CollectionView.ItemSizingStrategy を MeasureFirstItem に設定してもまだ重い。

https://github.com/dotnet/maui/issues/6317

BindableLayout でも同程度フリーズするので CollectionView だけの問題ではなさそう。
またプチフリーズ中何度も GC が走っているのも気になる。

回避策は無いが ObservableColection に 1 件 Add しては await Task.Yield() すれば一応そういうアニメーションみたいに見えるので、それでお茶を濁す必要があるか。

ProudustProudust

6.0.547 (SR7) 現在、Android で Picker に任意の文字を入力できてしまう。
ソフトキーボードだとよほどの低スぺ端末でないと再現が難しいが、物理キーボードを繋げると簡単に再現できる。

https://github.com/dotnet/maui/issues/8265

ProudustProudust

6.0.547 (SR7) 現在、Android で Image を含む Page を表示すると稀に Java.Lanf.RuntimeException が発生する。
再現方法はよく理解できてないが、.NET で言う Dispose 済みのインスタンスを使おうとしたみたいなことが起きている模様。

https://github.com/dotnet/maui/pull/11787

Hidden comment
Hidden comment
Hidden comment
ProudustProudust

7.0.59 (SR 3) で修正された模様。
ただし アニメーション GIF のみ別の不具合でちゃんと再表示できないらしい?要調査

ProudustProudust

6.0.547 (SR7) 現在、Android で通知から PendingIntent を使って Activity を起動しようとした場合、Shell の内部でぬるぽが発生する。
7.0.0 RC2 では修正済だが何故か 6.0.x にバックポートされていない。

https://github.com/dotnet/maui/issues/9090

しかも修正済の 7.0.0 RC2 でも今度は Java.Lang.IllegalStateException で落ちる。

https://github.com/dotnet/maui/issues/10776

ProudustProudust

どちらも MainActivity の Activity 属性に LaunchMode = LaunchMode.SingleTop を加えると直る模様。

  [Activity(
      Theme = "@style/Maui.SplashTheme",
      MainLauncher = true,
      ConfigurationChanges = ConfigChanges.ScreenSize
          | ConfigChanges.Orientation
          | ConfigChanges.UiMode
          | ConfigChanges.ScreenLayout
          | ConfigChanges.SmallestScreenSize
          | ConfigChanges.Density,
+     LaunchMode = LaunchMode.SingleTop
  )]
  public class MainActivity : MauiAppCompatActivity
  {
  }

https://github.com/dotnet/maui/issues/10776#issuecomment-1312531921

Intent 受信時の挙動が変わるので注意。

https://woshidan.hatenablog.com/entry/2016/01/18/083000

ProudustProudust

Application.Properties が削除されている。
代わりに Microsoft.Maui.Storage.Preferences を使用してほしいとのことだが、既に保存されているデータを移行する方法は用意されていない。
また、Preferences はサポートされている型が限られているため、System.Text.Json などでシリアライズしてから保存する必要がある。

https://github.com/dotnet/maui/issues/22

ProudustProudust

Application.OnAppLinkRequestReceived のデフォルト実装が削除され、空実装になっている。
Xamarin.Forms での実装をコピペすれば一応動くはずだが、削除に至った経緯などは不明。

https://github.com/xamarin/Xamarin.Forms/blob/8f765bd87a2968bef9c86122d88c9c47be9196d2/Xamarin.Forms.Controls/App.cs#L127-L154

ProudustProudust

Platforms/Android/values/colors.xml で定義した color を AndroidManifest.xml や MetaDataAttribute で参照しようとするとビルドエラー?
原因がよくわからないので要調査

ProudustProudust

6.0.548 (SR8) / VS2022 17.4.3 で Mac とペアリングした状態でリモートデバイスに対してデバッグ実行を行うと Type x:String not found in xmlns http://schemas.microsoft.com/winfx/2009/xaml で実行時例外。
7.0.52 (SR 1.1) に上げると問題なく実行できる。

https://github.com/dotnet/maui/issues/6070

ProudustProudust

7.0.52 (SR1.1) 現在、iOS で Shell アプリの戻るボタンの表示が変。
戻るボタンはタイトルの長さに応じて Back (戻る) またはページのタイトルが表示されるが、タイトルが表示された場合、タイトルが二つ並ぶ不思議な光景になる。

https://github.com/dotnet/maui/issues/11691
https://github.com/dotnet/maui/issues/8335

ProudustProudust

また iOS 14 から戻るボタン長押しで複数ページまとめて戻ることができるが、MAUI の Shell アプリで見るとすべて空白に見える。

Hidden comment
ProudustProudust

以下の PR で再修正された模様。
https://github.com/dotnet/maui/pull/12888

2023-03-23 時点では .NET 7 へバックポートされていないが、PureWeen.Maui.FixesAndWorkarounds を使用することで対策版のレンダラーに差し替えることができる。
ただし戻るボタン長押しで戻った場合に NavigationStack と不整合が起きてしまっているような気がする。(要調査)

I have a workaround renderer that I've put here

https://github.com/PureWeen/ShanedlerSamples/blob/main/ShanedlerSamples/Library/Workarounds/ShellWorkarounds.iOS.cs

Let me know if you hit any issues and I'll fix

引用元: https://github.com/dotnet/maui/issues/8335#issuecomment-1433488854

ProudustProudust

7.0.52 (SR1.1) 現在、iOS で Shell.TitleView を設定した場合、一度表示したページをタブ切り替えや戻るなどでもう一度表示させたときに表示が消えてしまう。
https://github.com/dotnet/maui/issues/9687

Hidden comment
ProudustProudust

以下の PR で再修正された模様。
https://github.com/dotnet/maui/pull/12888

2023-03-23 時点では .NET 7 へバックポートされていないが、PureWeen.Maui.FixesAndWorkarounds を使用することで対策版のレンダラーに差し替えることができる。

I have a workaround renderer that I've put here

https://github.com/PureWeen/ShanedlerSamples/blob/main/ShanedlerSamples/Library/Workarounds/ShellWorkarounds.iOS.cs

Let me know if you hit any issues and I'll fix

引用元: https://github.com/dotnet/maui/issues/8335#issuecomment-1433488854

ProudustProudust

7.0.52 (SR1.1) 現在、iOS で ScrollView.Content 内の要素を後から一部変更などして縦幅が伸びる場合、ScrollView も一緒に縦幅が伸びてスクロールできなくなる。
ScrollView.Content 内の要素を後から一部変更する後 HeightRequest を設定し直すと、縦幅が縮んでスクロールできるようになる。

ScrollArea.HeightRequest = 0;
ScrollArea.HeightRequest = -1d;
ProudustProudust

7.0.52 (SR1.1) 現在、LinearItemsLayout.ItemSpacing の挙動が Android と iOS で微妙に異なる。

Android: CollectionView.HeaderCollectionView.Items の間にも LinearItemsLayout.ItemSpacing適用される
iOS: CollectionView.HeaderCollectionView.Items の間には LinearItemsLayout.ItemSpacing適用されない

ProudustProudust

CollectionView.ScrollTo(0) の挙動も異なる。

Android: CollectionView.Items[0] までスクロール (CollectionView.Header は表示されない)
iOS: CollectionView.Header までスクロール

ProudustProudust

7.0.52 (SR1.1) 現在、iOS で CollectionView.Header 内の要素を後から一部変更などして縦幅が伸びる場合、項目と重なって表示される。
CollectionView.Header 直下の要素を変更後 HeightRequest を設定し直すと、縦幅が再計算され重ならなくなる。

((VisualElement)Collection.Header).HeightRequest = 0;
((VisualElement)Collection.Header).HeightRequest = -1d;

https://github.com/dotnet/maui/issues/12429

ProudustProudust

7.0.52 (SR1.1) 現在、Android で Border 内に Label を入れ、改行されないギリギリの横幅になるように Label.Text を設定すると、末尾数文字が表示されない場合がある。
Label.Height に連動して Border.Height も伸びるはずが正しく再計算されない場合がある模様。
Label.Text の末尾にスペースを入れると正しく再計算される。

ProudustProudust

7.0.52 (SR1.1) 現在、Android で CollectionView.EmptyView が表示されない。
CollectionView.EmptyView の HeightRequest を設定し直すと、縦幅が再計算され表示される。

((VisualElement)Collection.EmptyView).HeightRequest = 0;
await Task.Yeild();
((VisualElement)Collection.EmptyView).HeightRequest = -1d;

https://github.com/dotnet/maui/issues/11847

ProudustProudust

7.0.52 (SR1.1) 現在、Android で RadioButton の〇の色を変えるプロパティが存在しない。
MAUI で追加された Mapper 機能で変更できる。

RadioButtonHandler.Mapper.AppendToMapping("ButtonColor", (handler, element) => {
    var view = (AndroidX.AppCompat.Widget.AppCompatRadioButton)handler.PlatformView;
    var color = element.TextColor.ToPlatform(); // 暫定的に文字色と同じ色に変化させる
    view.ButtonTintList = Android.Content.Res.ColorStateList.ValueOf(color);
});

https://zenn.dev/muak/articles/8e17cabbc1ae35
https://stackoverflow.com/a/29776855

ProudustProudust

あえてダークテーマに対応しない場合、各 OS 向けのダークモード無効設定に加えて VisualElement.BackgroundColor などの初期値を上書きする必要があるらしい。

ProudustProudust

MAUI は特に何も指定しなければ .NET のバージョンに対応した最新バージョンが使用される。
この挙動が望ましくない場合は csproj に <MauiVersion> を追加することでバージョンを固定できるらしい。

https://github.com/dotnet/maui/blob/47f8d3ba07661801094578f2d842426ba8a1fed9/src/Workload/README.md#mauiversion

Hidden comment
ProudustProudust

7.0.86 (SR6) 現在、いつの間にか <MauiVersion> が再び機能するようになっていた。

ProudustProudust

7.0.86 (SR6) 現在、iOS で ScrollView を含むレイアウトが崩れる場合がある。
筆者の環境では Shell の TabBer 下まで ScrollView が拡大してしまう現象が多発していた。

現状回避策はなさそうだが、前述の <MauiVersion> で 7.0.58 に戻すととりあえずは大丈夫。

https://github.com/dotnet/maui/pull/14176