Xamarin 表单 - 在页面和页面之间传递数据意见 [英] Xamarin forms - Passing data between pages & views

查看:50
本文介绍了Xamarin 表单 - 在页面和页面之间传递数据意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xamarin 表单应用程序.有2个数据类,其中一页是填充数据.

I have a xamarin forms app. There are 2 classes with data, one of the pages is filling the data.

问题是:我正在创建新视图,该视图应该使用来自两个类的数据.

The problem is: I'm creating new view, that should use data from both classes.

我熟悉的唯一方法是将类设置为 bindingContext 以在页面之间传递数据,并且它与 ONE 类一起工作正常,因为显然有不能同时为 2 bindingContext.

The only way i'm familiar with is to set a class as a bindingContext to pass data between pages, and it's working fine with ONE class, because apparently there couldn't be 2 bindingContext at the same time.

示例:

第一节课(上一页所有的课都填好了,填完就接受)

1st class (all the classes are filled on the previous page. just accept that they are filled)

public class Buildings : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _id;

        public string Id
        {
            get { return _id; }
            set
            {
                _id = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Id"));
            }
        }
}

二级

public class Flats : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            private string _num;

            public string Num
            {
                get { return _num; }
                set
                {
                    _num = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Num"));
                }
            }
    }

新视图:

public partial class HouseView
    {
        private Flats _flats;
        private Buildings _buildings;
        public HouseView()
        {
            InitializeComponent();
        }

        private void HouseView_OnBindingContextChanged(object sender, EventArgs e)
        {
            var building = BindingContext as Building;
            //var flat = BindingContext as Flat;
            //_flat = flat;
            _building = building;
           var buildingInfo = await Rest.GetHouseInfo(_building.Id, _flat.Num); //function that will return info on a current house;
          // rest code
        }
    }

也许不需要绑定上下文,因为我只是传递参数,而不是在视图中更改它们?我想解决方案可能非常简单,我无法弄清楚......

Maybe there is no need for binding context, because i'm just passing the parameters, not changing them in a view? I guess the solution can be pretty simple, and i cant figure it out....

推荐答案

您缺少的是理解 ViewModel 的概念,以及它与视图的关系.. 在这种情况下,您需要的是第三类 (ViewModel)处理你之前的 2 堂课:

What you are missing is understanding the concept of ViewModel, and it's relation with the views.. In this case what you need is a 3rd class (ViewModel) that handles your 2 previous class:

public class HouseViewModel : INotifyPropertyChanged
{
    public Flats Flats { get; set; }
    private Buildings Buildings { get; set; }     
}

同时使用 OnBindingContextChanged 也很麻烦,并且会降低应用的一些性能.尝试在 VM 上准备数据之前,视图尽可能少地了解如何获取/处理数据.

Also using OnBindingContextChanged is just messy and will take some performance from your app .. try to prepare your data before on your VM, so the view knows as little as possible in how to get/handle data.

这篇关于Xamarin 表单 - 在页面和页面之间传递数据意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆