[C#][MAUI]コードビハインドでAppShellにShellContent追加 / Add ShellContent to AppShell in code-behind

C#

AppShellのShellContentをXAMLからではなく、C#コードで追加するにはAppShell.ItemsへShellContentオブジェクトを追加するとできるみたい。

以下は、MainPage(ShellContent)をAppShell.xaml.cs側で追加する。

C#
  public partial class AppShell
  {
      public AppShell()
      {
          InitializeComponent();

          ShellContent mainPage = new MainPage();
          this.Items.Add(mainPage);
      }
  }

実行結果: OK! MainPage (.NET MAUIのサンプルView)が表示された。

上記のC#コードは以下のXAMLと同じになっていると思われる。

XAML
<Shell
    x:Class="Sample.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Sample"
    Shell.FlyoutBehavior="Disabled"
    Title="Hello">
    <ShellContent ContentTemplate="{DataTemplate local:MainPage}" />
</Shell>

コメント

タイトルとURLをコピーしました