📖 wpf-frame-page-practice - Git Hub に置いたもの
「 WPFで どうやって画面遷移すんだぜ? ウィンドウだけでなく ダイアログボックスも遷移するぜ?」
📖 WPFとVisual Studioで画面遷移を実装する方法
「 👆 Frame と Page っていうのが あるようよ」
<Window x:Class="WpfFramePagePractice.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfFramePagePractice"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
<Frame Name="frame" NavigationUIVisibility="Hidden"/>
「 👆 Gridタグに 上記の Frame タグを挟めばいいらしいぜ」
<Window x:Class="WpfFramePagePractice.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfFramePagePractice"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame Name="frame" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
「 HTML にも iframe (インナーフレーム)ってタグが有ったしな。
ページの場所を指定したら ページが出てくるような場所を取ってるんじゃないか?」
<Page x:Class="WpfFramePagePractice.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfFramePagePractice"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
</Grid>
</Page>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Label FontSize="50" Grid.Row="0">Page1</Label>
<Button Grid.Row="1">ページ2へ遷移</Button>
「 👆 ページ1 の Gridタグの間に このコードを挟むらしいぜ」
<Page x:Class="WpfFramePagePractice.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfFramePagePractice"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Label FontSize="50" Grid.Row="0">Page1</Label>
<Button Grid.Row="1">ページ2へ遷移</Button>
</Grid>
</Page>
<Label FontSize="50">Page2</Label>
「 👆 ページ2には これだけ Gridタグに挟めばいいようだぜ」
<Page x:Class="WpfFramePagePractice.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfFramePagePractice"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page2">
<Grid>
<Label FontSize="50">Page2</Label>
</Grid>
</Page>
「 Frame と Page1 と Page2 が バラバラに在っても 何にもならないから
遷移をするコードを書く必要が あるはずなのよね」
Uri uri = new Uri("/Page1.xaml", UriKind.Relative);
frame.Source = uri;
「 👆 MainWindowのコンストラクタの InitializeComponent();
のあとに
上記の2行を追加すればいいみたいだぜ」
using System;
using System.Windows;
namespace WpfFramePagePractice
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Uri uri = new Uri("/Page1.xaml", UriKind.Relative);
frame.Source = uri;
}
}
}
「 👆 多分 Frame に Page1 が出てるんだろ これ」
「 👆 ボタンをクリックして選んで、プロパティ ウィンドウの 雷みたいなマークのボタンをクリックして、
Click とかいてある行の テキストボックスを ダブルクリックしろだぜ」
「 👆 Button_Click という名前のスケルトンができてるから、」
var page2 = new Page2();
NavigationService.Navigate(page2);
「 そういやお父ん ビルドすること 説明すんの よく忘れるよな」
Crieitは個人で開発中です。
興味がある方は是非記事の投稿をお願いします! どんな軽い内容でも嬉しいです。
なぜCrieitを作ろうと思ったか
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください!