[WPF] WPF 정리 2

jckim22·2024년 7월 6일

[C#] Study

목록 보기
4/4

화면이동 프레임

Main

<Window x:Class="페이지이동.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:페이지이동"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="5*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button FontSize="20" Name="뒤로" Click="뒤로_Click">&lt;</Button>
            <Button FontSize="20" Name="앞으로" Click="앞으로_Click">&gt;</Button>
        </StackPanel>

        <Frame Name="frm" Grid.Row="1"
               Source="/Page1.xaml"
               ></Frame>
    </Grid>
</Window>
namespace 페이지이동
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void 뒤로_Click(object sender, RoutedEventArgs e)
        {
            if (frm.NavigationService.CanGoBack)
            {
                frm.NavigationService.GoBack();
            }
        }

        private void 앞으로_Click(object sender, RoutedEventArgs e)
        {
            if (frm.NavigationService.CanGoForward)
            {
                frm.NavigationService.GoForward();
            }
        }
    }
}

Page1

<Page x:Class="페이지이동.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:페이지이동"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page1" Background="Salmon">

    <StackPanel>
        <Label FontSize="20">페이지1</Label>
        <Button Content="이동" Click="Button_Click"></Button>
    </StackPanel>
</Page>
namespace 페이지이동
{
    /// <summary>
    /// Page1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Page2 page2 = new Page2();
            NavigationService.Navigate(page2);
        }
    }
}

Page2

<Page x:Class="페이지이동.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:페이지이동"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page2" Background="LightBlue">

    <StackPanel>
        <Label FontSize="20">페이지2</Label>
        <Button Click="Button_Click">이동</Button>
    </StackPanel>
</Page>
namespace 페이지이동
{
    /// <summary>
    /// Page2.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Page2 : Page
    {
        public Page2()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Page3 page3 = new Page3();
            NavigationService.Navigate(page3);
        }
    }
}

Page3

<Page x:Class="페이지이동.Page3"
      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:페이지이동"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page3" Background="LightGreen">

    <StackPanel>
        <Label FontSize="20">페이지3</Label>
        <Button Click="Button_Click">첫화면</Button>
    </StackPanel>
</Page>
namespace 페이지이동
{
    /// <summary>
    /// Page3.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Page3 : Page
    {
        public Page3()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Page1 page1 = new Page1();
            NavigationService.Navigate(page1);
        }
    }
}
  • NavigationService를 통해 Navigate 되었던 페이지들이 기억이 되므로 Main에서 NavigationService로 이동을 자유롭게 할 수 있다.
profile
개발/보안

0개의 댓글