■ NavigationWindow 엘리먼트를 사용해 페이지를 탐색하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="Frame 엘리먼트 : 페이지 탐색하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Frame Grid.Row="0" Background="Green" Source="Page1.xaml" /> <Frame Grid.Row="1" Background="Gold" Source="Page2.xaml" /> </Grid> </Window> |
▶ Page1.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Page x:Class="TestProject.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="페이지 1" Title="페이지 1"> <Grid> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"> <Hyperlink NavigateUri="Page2.xaml">Page 2</Hyperlink>로 이동하기 </TextBlock> </Grid> </Page> |
▶ Page2.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<Page x:Class="TestProject.Page2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="페이지 2" Title="페이지 2"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"> RequestNavigate 이벤트를 사용해 <Hyperlink Name="hyperlink" NavigateUri="Page3.xaml"> 페이지 3</Hyperlink>으로 이동하기 </TextBlock> <Button Name="button" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="5"> 페이지 1로 이동하기 </Button> <TextBlock Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"> <Hyperlink NavigateUri="https://icodebroker.tistory.com"> ICODEBROKER 블로그</Hyperlink>로 이동하기 </TextBlock> </Grid> </Page> |
▶ Page2.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; namespace TestProject { /// <summary> /// 페이지 2 /// </summary> public partial class Page2 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Page2() /// <summary> /// 생성자 /// </summary> public Page2() { InitializeComponent(); this.hyperlink.RequestNavigate += hyperlink_RequestNavigate; this.button.Click += button_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 하이퍼링크 탐색 요청시 처리하기 - hyperlink_RequestNavigate(sender, e) /// <summary> /// 하이퍼링크 탐색 요청시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) { NavigationService.Navigate(e.Uri); e.Handled = true; } #endregion #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("Page1.xaml", UriKind.Relative)); } #endregion } } |
▶ Page3.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<Page x:Class="TestProject.Page3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="페이지 3" Title="페이지 3"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBox Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="25" /> <TextBlock Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10"> <Hyperlink NavigateUri="Page1.xaml">페이지 1</Hyperlink>로 이동하기 </TextBlock> </Grid> </Page> |