【问题标题】:How to transfer data from one page to another xamarin如何将数据从一个页面传输到另一个 xamarin
【发布时间】:2019-11-06 17:28:13
【问题描述】:

我有三页,我在第二页输入数据,传到第一页,同时返回,这个没问题,我用导航,像这样:

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,但到目前为止我还没有理解它是如何工作的。 请告诉我如何做得更好:)

【问题讨论】:

  • 一个页面只是一个 c# 类。您可以通过它的构造函数或公共属性或方法将数据传递给它。如果您需要从多个页面访问数据,您可以将其添加到您的 Application 对象,或使用静态类。

标签: xamarin mvvm xamarin.forms data-transfer


【解决方案1】:

通过构造函数传递数据:

在第 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);
    }
}

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

【讨论】:

  • 非常感谢,帮了大忙,但是又出现了一个问题,我需要动态添加对象到页面,我把它们存储在ObservableCollection中,问题是只保存了一个对象集合 - 最后一个,我在第二页添加对象,我需要在第三页查看所有添加对象的列表。
【解决方案2】:

我会在这里举一些例子

在第一页(谁叫第二页)

  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();

        }

    }

【讨论】:

  • 谢谢,我会努力的:)
  • ofc 这个作品,我一直在用,我会更新我的代码的答案
  • 已更新,如果您需要更多帮助,请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2017-01-25
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多