[WPF] PageFunction

C#/WPF 2020. 7. 9. 15:40
728x90

PageFunction 은 쉽게 설명하면 Navigation 구조에서 리턴이 있는 페이지 라고 보면 되겠다

회원가입절치를 진행할 때 주소입력시 주소를 조회하기위한 화면이 뜨고

주소검색이 끝나면 다시 원래 페이지로 돌아와 주소가 입력되게 되는데

주소를 조회하기 위한 화면이 PageFunction 으로 구현하면 된다.

 

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d"

        WindowStartupLocation="CenterScreen"

        Title="MainWindow" Height="400" Width="250">

    <Grid>

        <Frame Source="MainPage.xaml" />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

using System.Windows;

 

namespace WpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

    }

}

 

MainPage.xaml

<Page x:Class="WpfApp.MainPage"

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

      mc:Ignorable="d"

      d:DesignHeight="450" d:DesignWidth="250"

      Title="Page1" Background="White">

    <StackPanel>

        <StackPanel Orientation="Horizontal" Margin="5">

            <Label Content="이름 : " VerticalAlignment="Center" />

            <TextBox Background="LightGray" VerticalAlignment="Center" Width="100"/>

        </StackPanel>

        <Button Name="addressButton" Content="주소입력" Click="AddressButton_Click" />

        <StackPanel Orientation="Horizontal" Margin="5">

            <Label Content="주소 : " VerticalAlignment="Center" />

            <TextBlock Name="addressTextBlock" Background="LightGray" Width="200" VerticalAlignment="Center"/>

        </StackPanel>

    </StackPanel>

</Page>

 

 

* MainPage 에서 PageFunction 페이지를 호출하고 입력값을 리턴 받는다.

 

MainPage.xaml.cs

using System.Windows.Controls;

using System.Windows.Navigation;

 

namespace WpfApp

{

    public partial class MainPage : Page

    {

        public MainPage()

        {

            InitializeComponent();

        }

 

        /// <summary>

        /// 주소입력 버튼 클릭이벤트입니다.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void AddressButton_Click(object sender, System.Windows.RoutedEventArgs e)

        {

            InputAddressPage inputAddressPage = new InputAddressPage();

 

            inputAddressPage.Return += new ReturnEventHandler<string>(InputAddressPage_Return);

 

            NavigationService.Navigate(inputAddressPage);

        }

 

        /// <summary>

        /// InputAddressPage 가 닫혔을 때 결과값을 받아 처리하는 이벤트입니다.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        void InputAddressPage_Return(object sender, ReturnEventArgs<string> e)

        {

            this.addressTextBlock.Text = e.Result;

        }

    }

}

 

 

InputAddressPage.xaml

<PageFunction x:Class="WpfApp.InputAddressPage"

              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

              xmlns:sys="clr-namespace:System;assembly=mscorlib"

              x:TypeArguments="sys:String"

              mc:Ignorable="d"

              d:DesignHeight="300" d:DesignWidth="300"

              Title="InputAddressPage" Background="Yellow">

    <StackPanel>

        <TextBlock Name="addressTextBlock" Text="주소를 입력하세요" VerticalAlignment="Center"/>

        <TextBox Name="addressTextBox" VerticalAlignment="Center"/>

        <Button Name="inputButton" Content="입력" Click="InputButton_Click" />

    </StackPanel>

</PageFunction>

 

 

InputAddressPage.xaml.cs

using System.Windows;

using System.Windows.Navigation;

 

namespace WpfApp

{

    public partial class InputAddressPage : PageFunction<string>

    {

        public InputAddressPage()

        {

            InitializeComponent();

        }

 

        /// <summary>

        /// 입력 버튼 클릭시 이벤트입니다.

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void InputButton_Click(object sender, RoutedEventArgs e)

        {

            // MainPage 로 값을 넘겨줍니다.

            OnReturn(new ReturnEventArgs<string>(this.addressTextBox.Text));

        }

    }

}

 

 

       

 

 

 

728x90
Posted by kjun.kr
,