I Am working on xamarin PCL forms app, i was trying to send data from content page to tabbed page. Here i have content page code below
private async void StudentList_ItemTapped(object sender, ItemTappedEventArgs e)
{
var student = StudentList.SelectedItem as Students;
if (student != null) {
var mainViewModel = BindingContext as StudentsViewModel;
if (mainViewModel != null) {
mainViewModel.SelectedStudent = student;
await Navigation.PushAsync(new ProfilePage(mainViewModel));
}
}
}
Now, this ViewModel has property which has getter and setter method implemented which is getting value. My logic in this code is to set value of selecteditem from list and get object of selected person's data. and access that person's data on tabbed page to show it in profile.
Below id tabbed-page.cs
public partial class ProfilePage : TabbedPage
{
public ProfilePage()
{
InitializeComponent();
}
public ProfilePage(StudentsViewModel mainViewModel)
{
InitializeComponent();
BindingContext = mainViewModel;
}
}
if you see its possible to get selected item value when you pass your view-model (which has property to set object value) into parameters by setting it up as binding context and accessing it on another content page by catching it by setting parameter value as same as passed view-model.
Here, my question is how can i achieve that same technique when i am using content page to tabbed page instead content page to content page.
Thanks in advance, please let me know if you do not have idea or you ar what i want to discuss here.
Basically your tabbed page holds three content pages (let's assume you have three tabs).
The most straightforward way would be to create a model for your tabbed page, which holds the models for the three subpages.
Give the subpages of the tabbed page names to make them accessible in codebehind and then in the constructor of the tabbed page, set the binding contexts of your subpages to the corresponding model:
public TabPageModel
{
public Page1Model ModelPg1 {get;set;}
public Page2Model ModelPg2 {get;set;}
public Page3Model ModelPg3 {get;set;}
}
public partial class MyTabbedPage : TabbedPage
{
public MyTabbedPage()
{
InitializeComponent();
}
public MyTabbedPage(TabPageModel model) : this()
{
this.SubPage1.BindingContext = model.ModelPg1;
this.SubPage2.BindingContext = model.ModelPg2;
this.SubPage3.BindingContext = model.ModelPg3;
}
}
Even though I omitted it in the pseudocode above, make sure your main model (and also the submodels) implement INotifyPropertyChanged.