2017-12-01 6 views
2

のパブリック変数を取得しますこの値を変更できます。は、私は別のページをインスタンス化していて、私はこのようなそのパブリックプロパティ(「someValueの」)のいずれかに値を代入ポップページ

"_otherpage"がポップされたとき、私は "SomeValue"変数を見て、それに何かしたいと思います。

私は値の変更について通知されたくないので、MessagingSystemは必要なものではありません。私は "_otherpage"がポップされたときにこの値が何であるかを知りたいだけです。

私はこのような変数の多くを扱っているときに整理するのが難しいと感じているので、私はBinding(可能ならば!)も使用したくありません。

おそらくイベントでこれを行うことはできますか?

私の夢のソリューションは、(擬似コード)のようになります。

private void OnPagePopped() 
{ 
    int iNewValue = PoppedPage.SomeValue; 
} 

ありがとうございました。ページAでは

+0

メッセージングセンターを使用できない理由はありません。または、昨日カスタムイベントを扱う質問をしただけで、それを行うことができます。 – Jason

答えて

0

:ページBでは

MessagingCenter.Subscribe<string>(this, "SomeMessage", (s) => HandleMessage(s)); 

:ページBで

MessagingCenter.Send<string>("SenderOrAnythingElse", "SomeMessage"); 
+0

私は返事を「正しい」回答としませんが、これはあまりにも醜いと感じるからです。その他のオプションは大歓迎です。私は、うまくいくものを見せたいと思っていました。 – tmighty

0

パブリック変数がある:

public int SomeValue { get; set; }     

は、今、私たちは、ページBを示しています。

PageB nPageB = new PageB; 
await Navigation.PushAsync(nPageB); 
nPageBで

は、ユーザーが今これは私が私のポップアップでそれを行う方法ですが、それは同じようにページスタイルで使用することができます

//we track the "Disappearing" event of the page. 
//When it occurs, we get the value 

nPageB.Disappearing += async (sender1, e1) => 
{ 
    Debug.WriteLine("New value is: " + nPage.SomeValue.ToString()); 
}; 
+0

@SushiHangoverあなたのアドバイス/コメントは、これで本当に感謝しています。 – tmighty

+0

これはうまくいきますが、かなり貧弱なデザインです - 2つのページが密接に結合されていますが、これは一般的には悪い考えです – Jason

1

にページBのパブリック変数を変更することができます。

相続人は私の小さな例、それは公共の財産が座っているページ上Taskを持っていることについて基本的にMainPage = new NavigationPage(new Page1());

を期待。このタスクは、publicプロパティの値を返すことができます。タスクは、OnDisappearingオーバーライドで完了し、パブリックプロパティを返します。

戻って値を取得するには、ページを押して

using System; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace MVVMTests 
{ 
    /// <summary> 
    /// A base implementation for a page, that holds a task, 
    /// which can be completed and returns a value of type of the generic T 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    public class ResultContentPage<T> : ContentPage 
    { 
      public Task<T> PagePoppedTask { get { return tcs.Task; } } 
      private TaskCompletionSource<T> tcs; 

      public ResultContentPage() 
      { 
       tcs = new TaskCompletionSource<T>(); 
      } 

      /// <summary> 
      /// Completes the task and sets it result 
      /// </summary> 
      /// <param name="result"></param> 
      protected /* virtual */ void SetPopupResult(T result) 
      { 
       if (PagePoppedTask.IsCompleted == false) 
        tcs.SetResult(result); 
      } 
    } 

    /// <summary> 
    /// Page1 exists of one button, that creates the Page2, assigns a value to a prop on the Page2 
    /// and then awaits the PagePoppedTask of Page2 
    /// </summary> 
    public class Page1 : ContentPage 
    { 
     public Page1() 
     { 
      var button = new Button() 
      { 
       Text = "Go to page 2" 
      }; 
      button.Clicked += Button_Clicked; 

      Content = button; 
     } 

     private async void Button_Clicked(object sender, EventArgs e) 
     { 
      //Push the page 
      var newPage = new Page2() { CoolInt = 123 }; 
      await App.Current.MainPage.Navigation.PushAsync(newPage); 

      //Await the result 
      int result = await newPage.PagePoppedTask; 
      System.Diagnostics.Debug.WriteLine("Page result: " + result.ToString()); 
     } 
    } 

    /// <summary> 
    /// Inherits from the ResultContentPage and sets the PagePoppedTask as soon as its Disappearing 
    /// </summary> 
    public class Page2 : ResultContentPage<int> 
    { 
     public int CoolInt { get; set; } //Your property on the page 

     public Page2() 
     { 
      var button = new Button() 
      { 
       Text = "Go back to page 1" 
      }; 
      button.Clicked += Button_Clicked; 

      Content = button; 
     } 

     private async void Button_Clicked(object sender, EventArgs e) 
     { 
      CoolInt = 321;          //assign dummy value to CoolInt prop and pop the page 
      await App.Current.MainPage.Navigation.PopAsync(); //pop the page 
     } 

     protected override void OnDisappearing() 
     { 
      base.OnDisappearing(); 
      SetPopupResult(CoolInt); //set the result of the task (in ResultContentPage<T>) 
     } 
    } 
} 
0

タスクpage.PagePoppedTaskをお待ちしておりますが、理想的な解決策を探しているなら、私はMVVMパターンに従って、あなたの多くを移動することをお勧めあなたのページからビューモデルへのコード

私はFreshMvvmというMVVMフレームワークを使用します。これは私がモデルのナビゲーションを表示するビューモデルを実行すると、このようにそれらの間にパラメータを渡すことができます:

await CoreMethods.PushPageModel<BPageModel>(myParameter, true); 

これは私がBPAGEビューモデルのInitメソッドにアクセスすることができBPAGEにmyParameter渡します。

私は私はAPageViewModelのReverseInit方法でアクセスすることができAページ

await CoreMethods.PopPageModel(myReturnParam, true); 

に戻すパラメータを渡すことができる(ビューモデルを介して)Bのページを開くとき。

ほとんどのMVVMフレームワークは、同様の機能を備えています。

Here are more details about FreshMvvm

関連する問題