2017-03-04 4 views
0

사용자가 각 페이지의 텍스트 상자 몇 개를 채우는 Xamarin 휴대용 클래스 라이브러리 (대상 플랫폼 UWP)에서 앱을 만들었습니다. 이 정보를 xml 파일의 마지막 페이지 (버튼 클릭시)에 저장해야하므로 마지막 페이지로 각 페이지를 통해 정보를 전달해야합니다. 어떻게해야합니까? 여기여러 페이지를 통한 인수 전달

내 직렬화 가능 클래스 : 여기

namespace myProject 
{ 
[XmlRoot("MyRootElement")] 
     public class MyRootElement 
     { 
      [XmlAttribute("MyAttribute1")] //name of the xml element 
      public string MyAttribute1  //name of a textboxt e.g. 
      { 
       get; 
       set; 
      } 
      [XmlAttribute("MyAttribute2")] 
      public string MyAttribute2 
      { 
       get; 
       set; 
      } 
      [XmlElement("MyElement1")] 
      public string MyElement1 
      { 
       get; 
       set; 
      } 
} 

내 첫 페이지 :

namespace myProject 
{ 
    public partial class FirstPage : ContentPage 
    { 
     public FirstPage() 
     { 
      InitializeComponent(); 
     } 
     async void Continue_Clicked(object sender, EventArgs e) 
     { 
      MyRootElement mre = new MyRootElement 
      { 
       MyAttribute1 = editor1.Text, 
       MyAttribute2 = editor2.Text, 
       MyElement1 = editor3.Text 
      }; 
      await Navigation.PushAsync(new SecondPage(mre)); 
     } 
    } 
} 
(나는 그것이 잘못된 것 같아요있어) 여기에 도움이 사용자에 의해 제안

두 번째 페이지는 다음과 같습니다 : 파일이 생성 된 마지막 페이지에

namespace myProject 
{ 
    public partial class SecondPage : ContentPage 
    { 

     public MyRootElement mre { get; set; } 

     public SecondPage(MyRootElement mre) 
     { 
      this.mre = mre; 
      InitializeComponent(); 
     } 

     async void Continue2_Clicked(object sender, EventArgs e) 
     { 
      MyRootElement mre = new MyRootElement 
      { 
       someOtherElement = editorOnNextPage.Text 
      }; 
      await Navigation.PushAsync(new SecondPage(mre)); 
     } 
    } 
} 

:

,
namespace myProject 
{ 
    public partial class LastPage : ContentPage 
    { 
     private MyRootElement mre { get; set; } 

     public LastPage(MyRootElement mre) 
     { 
      this.mre = mre; 
      InitializeComponent(); 
     } 

     private async void CreateandSend_Clicked(object sender, EventArgs e) 
     { 
      var s = await DependencyService.Get<IFileHelper>().MakeFileStream(); //stream from UWP using dependencyservice 

      using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8)) 
      { 
       XmlSerializer serializer = new XmlSerializer(typeof(MyRootElement)); 
       serializer.Serialize(sw, mre); 
      } 
     } 
    } 
} 

내 질문에 답하기 위해 더 많은 내용이 필요하면 알려주십시오.

답변

1

첫 페이지에만 MyRootElement의 인스턴스를 한 번만 만들어야합니다. 그런 다음 후속 페이지의 동일한 인스턴스를 계속 사용하십시오.

async void Continue2_Clicked(object sender, EventArgs e) 
{ 
    // use the same copy of mre you passed via the construt 
    this.mre.someOtherElement = editorOnNextPage.Text 

    await Navigation.PushAsync(new SecondPage(mre)); 
} 
+0

! 고마워요, 선생님! – RoloffM