Better way to share parent ViewModel between all child TABS

kalkotekedarkalkotekedar USMember ✭✭
edited December 2018 in Xamarin.Forms

I have a scenario where One page contains 4 tabs. I want to share the parent ViewModel in all four tabs. what is the best way to do that?
Right now I have created by extending the parent ViewModel and On click of Icon on parent page it should check all the page values and store it.

You can see the above image will show what I want to achieve
I have Detail Page, Page A, Page B, Page C, and Page D. Details page ViewModel is extending in all four pages also I have added an event in the parent ViewModel which will trigger when you click on the home icon on the toolbar. I am not getting the event trigger in my child page and also getting the parent ViewModel variables null in the child tab page.

What is the best solution to handle such kind of scenarios? Thanks in advance !!!

@NMackay I hope you have faced such kind of scenarios.
@BrianLagunas I am using the Prism library. What is the better way to handle in the prism?
@JamesMontemagno

Answers

  • NMackayNMackay GBInsider, University mod

    @kalkotekedar

    Mmmm this suggests the design has gone wrong but your description is vague, in in case, in this kind of scenario you'd possibly have to use EventAggregator.

    By event handler do you mean OnNavigatedFrom? OnNavigatedTo & OnNavigatedFrom will only fire in the 1st child tab of your TabbedPage by design, you can easily add this behavior by doing something like

    public void OnNavigatedTo(INavigationParameters parameters)
            {
                if (parameters.GetNavigationMode() == NavigationMode.New)
                {
                    // Prism always raises OnNavigatedTo on 1st tabbed page so this prevents the first tab being initialised twice
                    if (Children.Count == 1)
                    {
                        return;
                    }
                    for (var pageIndex = 1; pageIndex < Children.Count; pageIndex++)
                    {
                        var page = Children[pageIndex];
                        (page?.BindingContext as INavigationAware)?.OnNavigatedTo(parameters);
                    }
                }
            }
    
    

    You really need to provide more info and a demo project of what your trying to achieve, also check out the TabbedPage sample on github for Prism

    https://github.com/PrismLibrary/Prism-Samples-Forms/tree/master/TabbedNavigation
    http://prismlibrary.github.io/docs/xamarin-forms/navigation/working-with-tabbedpages.html

  • kalkotekedarkalkotekedar USMember ✭✭
    edited December 2018

    I am sharing the code please find the attachment.

    On click of save all four page save method should trigger

    Tabs.zip 617.1K
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭

    Does a tabbed page's BindingContext not forward along to its children?

    Just set the Tabbed Page BindingContext to the ViewModel you want the children to have and see if it is inherited appropriately.

  • kalkotekedarkalkotekedar USMember ✭✭

    @AdamMeaney said:
    Does a tabbed page's BindingContext not forward along to its children?

    Just set the Tabbed Page BindingContext to the ViewModel you want the children to have and see if it is inherited appropriately.

    Right now I am extended the child VM with the parent VM.

    How can I forward to children?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭

    I would expect it to work just like it does for Views.

    public class TabPage : TabbedPage
    {
        public TabPage()
        {
            BindingContext = new SomeViewModel();
            Children.Add(new KidPage());
            Children.Add(new KidPage());
            Children.Add(new KidPage());
        }
    
        /// <summary>When overridden, allows application developers to customize behavior immediately prior to the <see cref="T:Xamarin.Forms.Page" /> becoming visible.</summary>
        /// <remarks>To be added.</remarks>
        protected override void OnAppearing()
        {
            base.OnAppearing();
            var viewModelIsInherited = Children.First().BindingContext == this.BindingContext;
            if (viewModelIsInherited)
            {
                System.Diagnostics.Debug.WriteLine("Yay, it works");
            }
        }
    }
    
  • NMackayNMackay GBInsider, University mod

    @kalkotekedar said:

    @AdamMeaney said:
    Does a tabbed page's BindingContext not forward along to its children?

    Just set the Tabbed Page BindingContext to the ViewModel you want the children to have and see if it is inherited appropriately.

    Right now I am extended the child VM with the parent VM.

    How can I forward to children?

    I haven't had a chance to check this out as I'm really busy but this sounds really odd behavior for a Prism app.

  • NMackayNMackay GBInsider, University mod

    Anyway, why are you sharing a parent viewmodel, just register a singleton service in the IoC and inject it in the child viewmodels and you could raise an event on the interface that you can handle that event in the parent, this all smells of bad design I'm sorry to say unless I'm missing something.

  • kalkotekedarkalkotekedar USMember ✭✭

    @NMackay said:
    Anyway, why are you sharing a parent ViewModel, just register a singleton service in the IoC and inject it in the child ViewModel and you could raise an event on the interface that you can handle that event in the parent, this all smells of bad design I'm sorry to say unless I'm missing something.

    Because I have the same object bind on four tabs and some other variables therefore I am sharing the ViewModel.
    If possible can you share an example or code snippet to understand well

Sign In or Register to comment.