如何将数据从一页传输到另一个xamarin

时间:2019-11-06 17:28:13

标签: xamarin mvvm xamarin.forms data-transfer

我有三页,我在第二页上输入数据,并将其传输到第一页,然后同时返回,这没有问题,我使用导航,如下所示:

private async void OnSaveTitleButtonCliked(object sender, EventArgs e)
    {
        var title_data = new LabelViewModel
        {
            Label = editor.Text,
            Date = DateTime.Now
        };
        var mainpage = new MainPage();
        mainpage.BindingContext = title_data;
        await Navigation.PushAsync(mainpage);

    }

但是我还需要将此数据传输到第三页,以便可以从第一页转到那里,看到我尝试了mvvm,但是到目前为止我还不了解它是如何工作的。 请告诉我如何做得更好:)

2 个答案:

答案 0 :(得分:0)

我在这里举一些例子

在第一页上(称为第二页)

  private async void MenuLista(object sender, EventArgs e)
        {
            var item = (ModelosPPP)((Button)sender).BindingContext;
            if (PopupRunnning != false)
                return;

            var page = new MenuListSV(item);
            PopupRunnning = true;

            page.Action += async (a, b) =>
            {
                switch (b)
                {
                    case 1:
                        await DisplayAlert("PDF", null, "ok");
                        break;
                    case 2:
                        await DisplayAlert("Reenviar", null, "ok");
                        break;
                    case 3:
                        await DisplayAlert("Excluir", null, "ok");
                        break;
                }

            };

            page.Disappearing += (c, d) =>
            {
                PopupRunnning = false;

            };

            await PopupNavigation.Instance.PushAsync(page);

        }

第二页

public partial class MenuListSV : PopupPage
    {
        public MenuListSV(Models.ModelosPPP obj)
        {
            InitializeComponent();
            BindingContext = obj;
        }


        public EventHandler<int> Action;

        public async void MenuChoice(object sender, EventArgs e)
        {
            var btn = sender as Button;
            switch (btn.Text)
            {
                case "Abrir PDF":
                    Action?.Invoke(this, 1);
                    break;
                case "Reenviar":
                    Action?.Invoke(this, 2);
                    break;
                case "Excluir":
                    Action?.Invoke(this, 3);
                    break;

            }
            await PopupNavigation.Instance.PopAsync();

        }

    }

答案 1 :(得分:0)

通过构造函数传递数据:

在第1页中:

private async void GoToPage2(object sender, EventArgs e)
{
    var title_data = new LabelViewModel
    {
        Label = editor.Text,
        Date = DateTime.Now
    };

    //Pass the model here
    var Page2 = new Page2(title_data);           
    await Navigation.PushAsync(Page2);

}

在第2页中:

public partial class Page2 : ContentPage
{

    public LabelViewModel model;

    public Page2(LabelViewModel m) {

        InitializeComponent();

        this.model = m;
        //You can use your model here

    }
}

按公共财产传递数据:

在第1页中:

private async void GoToPage2(object sender, EventArgs e)
{
    var title_data = new LabelViewModel
    {
        Label = editor.Text,
        Date = DateTime.Now
    };
    var Page2 = new Page2();
    //Pass the model here
    Page2.model = title_data;
    await Navigation.PushAsync(Page2);

}

在第2页中:

public partial class Page2 : ContentPage
{

    public LabelViewModel model;
    public Page2()
    {
        InitializeComponent();

        //You can use your model here
        Console.WriteLine(model.Label);
        Console.WriteLine(model.Date);
    }
}

如果您有任何疑问,请告诉我。