일단 NavigationService 을 이용해서 데이터 넘기는법부터 ㄱㄱ

image

데이터를 주고 받는데 사용되는 Person

namespace TestWpfBrowserApplication
{
    public  class Person
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _Age;

        public string Age
        {
            get { return _Age; }
            set { _Age = value; }
        }
    }
}

 

NavigationService으로 페이지 이동

Page1.xaml 에서 “회원정보입력으로 이동”을 누르면 Page2.xaml로 이동한다.

image

image

page2에서 받은값을 돌려주고 있다.

image

image

 

 

Page1.xaml

namespace TestWpfBrowserApplication
{
    /// <summary>
    /// Page1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
        public Page1(Person p) : this()
        {
            this.label1.Content = p.Name;
            label2.Content = p.Age;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            //PageFunction1 page = new PageFunction1();            
            //page.Return += new ReturnEventHandler<Person>(page_Return);            

            Page2 p2 = new Page2();
            this.NavigationService.Navigate(p2);
        }

        void page_Return(object sender, ReturnEventArgs<Person> e)
        {
            //결과값을 뿌려주고 있다.
            //Person person = e.Result as Person;
            //this.label1.Content = person.Name;
            //label2.Content = person.Age;

        }
    }
}

Page2.xaml

namespace TestWpfBrowserApplication
{
    /// <summary>
    /// Page2.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Page2 : Page
    {
        public Page2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Person person = new Person() { Age = this.txtAge.Text, Name = this.txtName.Text };
            Page1 p1 = new Page1(person);
            this.NavigationService.Navigate(p1);
        }
    }
}

 

 

전반적으로 NavigationService를 이용해서 간단히 페이지를 이동할 수 있다

private void button1_Click(object sender, RoutedEventArgs e)
{
   this.NavigationService.Navigate(new Page2());
}

 

 

 

 

하지만 위와 같은 방법은 매번 페이지를 생성하기 때문에 데이터관리가 까다롭다

그래서 데이터관리를 하면서 이동 할 수 있는 PageFunction 를 이용한다.

 

 

 

 

image

 

회원정보를 입력으로 이동 클릭하면 PageFunction1.xaml 로 이동한다

image 

입력완료를 누르면 다시 Page1.xaml 으로 돌아간다 이때 Person 데이터를 가지고 돌아간다

image

받아온 결과값을 뿌려주고 있다

image

 

Page1.xaml

namespace TestWpfBrowserApplication
{
    /// <summary>
    /// Page1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
        
            PageFunction1 page = new PageFunction1();            
            page.Return += new ReturnEventHandler<Person>(page_Return);            
            this.NavigationService.Navigate(page);



        }

        void page_Return(object sender, ReturnEventArgs<Person> e)
        {
            //결과값을 뿌려주고 있다.
           Person person = e.Result as Person;
           this.label1.Content = person.Name;
           label2.Content = person.Age;

        }
    }
}

PageFunction1.xaml

Person 형식이므로
image 
TypeArguments를 맞춰준다.
image 


namespace TestWpfBrowserApplication
{
    /// <summary>
    /// PageFunction1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class PageFunction1 : PageFunction<Person>
    {
        public PageFunction1()
        {
            InitializeComponent();                     
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Person person = new Person() {  Age=this.txtAge.Text , Name = this.txtName.Text };

            ReturnEventArgs<Person> returnObject = new ReturnEventArgs<Person>(person);
            OnReturn(returnObject);
        }

    
    }

}

 

로그인같은 단발성 UI 는 걍 NavigationService을 이용해서 처리하고

처리하고 회원가입같은 정보를 받아와야하는 경우에 PageFunction을 사용하면 좋겠다

 

물론

this.NavigationService.Navigate(new Page1(Person)); // 이렇게 해도 뭐…. ㅋ

 

참고 URL

http://msdn.microsoft.com/ko-kr/library/ms741843(v=VS.100).aspx

+ Recent posts