如何将数据从Navigation.PopAsync传递到上一页?

时间:2017-05-19 16:40:37

标签: c# xamarin

我已经看到了有关在导航页面之间传递数据的其他问题,但我似乎无法将Page2从Page2传递给Page1。

我有Page1推送另一个推送Page2,当在Page2上点击按钮时,我弹出内容页面并需要将整数传递给Page1。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:2)

我在事件和代表上看到了video并让它运行了!

第1页:

public partial class Page1 : ContentPage, INotifyPropertyChanged
{
    public static int _player;
    public static int Player
    {
        get{ return _player; }
        set{ _player = value; }
    }

    public NewGameH(string homeTeam, string opponentTeam)
    {
        InitializeComponent();
    }

    public void OnPlayerSet(object source, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Begin Player {0} ", Player);
    }

    async void button1_Clicked(object sender, System.EventArgs e)
    {
        var page2 = new Page2();
        page2.PlayerSet += this.OnPlayerSet;
        await Navigation.PushAsync(page2);
    }
}

2页:

public partial class Page2 : ContentPage{
    public delegate void SetPlayerEventHandler(object source, EventArgs args);
    public event SetPlayerEventHandler PlayerSet;

    public Page2()
    {
        InitializeComponent();
    }

    protected virtual void OnPlayerSet(){
        if (PlayerSet != null){
            PlayerSet(this, EventArgs.Empty);
        }
    }

    async void button2_Clicked(object sender, System.EventArgs e)
    {
        Page1.Player=1;
        OnPlayerSet();
        await Navigation.PopAsync();
    }
}