我有一个 xamarin 表单应用程序。有 2 个带有数据的类,其中一页正在填充数据。
问题是:我正在创建新视图,它应该使用来自两个类的数据。
我熟悉的唯一方法是将一个类设置为bindingContext
在页面之间传递数据,并且它与ONE类一起工作正常,因为显然不能2 bindingContext
同时存在。
例子:
1st class(所有班级都在上一页填写。接受他们已填写)
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
}
}
也许不需要绑定上下文,因为我只是传递参数,而不是在视图中更改它们?我想解决方案可能很简单,我想不通....