MainPage を ASP.NET の MasterPage と同じように使用する場合、MainPage には APP バー定義が必要であり、本文に単一フレーム要素のみを含める必要があります。このパターンを使用して、代わりにアプリのコンテンツを設定します
Window.Current.Content = // An Application Page
use
AppFrame.Content = //An Application Page
また、MainPage 要素から Mainpage コードを削除してカスタム ユーザー コントロールに配置することも検討してください。その後、MainPage の userControl からイベントを発生させて処理することができます。また、ロジックと UI を完全に再作成することなく、アプリの他の場所でその機能を使用することもできます。
MainPage の XAML の例を次に示します。
>
<Page.Resources>
<ResourceDictionary x:Name="CommonStyles" Source="/Common/StandardStyles.xaml" />
</Page.Resources>
<Page.TopAppBar>
<AppBar x:Name="NavigationAppBar" Padding="10,0,10,0" AutomationProperties.Name="Global App Bar" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="LeftCommands" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
<!-- App Bar Buttons Removed -->
</StackPanel>
</Grid>
</AppBar>
</Page.TopAppBar>
<!--
This grid acts as a root panel for the page that defines two rows:
* Row 0 contains the back button and page title
* Row 1 contains the rest of the page layout
-->
Getter と Setter を使用して、タイプ Frame のパブリック プロパティをアプリケーション ビュー モデルに追加します。
public Frame SelectedAppFrame {get;set;}
MainPage.xaml.cs ファイルでプロパティを割り当てます。
ApplicationViewModel vm = this.PageBackgroundGrid.DataContext as ApplicationViewModel;
vm.SelectedAppFrame = this.AppFrame;
アプリビューモデルでの一般的なナビゲートのコードは次のとおりです。
public void HandleNavigaitionEvent(object sender, string pageName, Frame AppFrame, StackPanel stack)
{
var content = Pages.Where(i => i.Name == pageName).FirstOrDefault();
NavigateTrigger(AppFrame, content);
}
public void NavigateTrigger(Frame AppFrame, LayoutAwarePage content)
{
EventAggregator.GetEvent<PageNavigatedEvent>().Publish(content);
AppFrame.Content = content;
NaviagationPath.Add(content);
}
そうすれば、ApplicationViewModel にアクセスできるアプリケーションのどこからでも変更を AppFram に反映できます (どこにでもあるはずです)。