2
votes

Ok so this one looks like its a simple one, but I guess one would have to be very familiar with the xamarin and the XAML sintax and know what it's capable of.

Scenario

I'm using Xamarin with Prism, So I have the power of the ViewModelLocator Autowire, so please keep this in mind when submitting your responses.

Ok Lets say I have a ContentPage (MainPage) and this page has a child component that is of type ContentView (CustomNavBar). For the ContentPage

Now Prism is cool in a way that I can register my view models through convention or custom, for this example lets register them explicitly (just for the sake of certanty)

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //Registering Navigation
        containerRegistry.RegisterForNavigation<NavigationPage>();
        containerRegistry.RegisterForNavigation<MainPage>, MainPageViewModel>();
        //I wish i could register the model of an object of type 
        //ContentView but I cant :( hence this post :P
    }

For the sake of clarity I can confirm that my ContentPagem.xaml is bound properly to its view model, Now since I cant bind a model to my ContentView (ChildView) with the code above, the next logical solution would be to inject the binding context from the parent to the child which works fine if you do something like:

<!--Snippet from my MainPage.xaml (ContentPage)-->
<local:CustomNavBar BindingContext="{Binding CurrentUser}" />

So this also works fine, and my childView (CustomNavBar.xaml) will have access to all the properties of the CurrentUser object

So My Question is If the MainPage.xaml (The contentpage / parent) has a Delegate Command I would like to pass down to the childView (CustomNavBar.xaml) then how would I go about passing it? Ideally i would like to pass the same viewmodel of the ContentPage (MainPage.xaml) down to the ChildView (CustomNavBar.xaml) in other words how do i pass in the bindingcontext to be the current view?

<!--How do i pass the current viewmodel to this contentview-->
<local:CustomNavBar BindingContext="{Binding <this viewmodel>}" />

I've solved my issue by wrapping the delegate command in an object and then passing that object down to the contentview but my question is how to do this cleaner and properly by passing the current viewmodel down to the child?

Note: I hope I set the context right, please avoid suggestions that go around the question like: "why dont you just register to an event with the IEventAggregator blah blah or any other suggestion. What I'm asking is clear" Any insights on this will be highly appreciated :) thanks.

1
Its so lonely...Egli Becerra

1 Answers

-1
votes

So to make sure that i understand you well , your problems is that you have Page A that has View 1, and you want to pass the BindingContext from PageA to View1 , right ?

Well in this case you do not have to do any thing as once you include View1 into PageA then the view will inherent the binding Context

<ContentPage Title="{Binding MyTitle}">
  <StackLayout IsVisibile="{Binding IsGroup1Showen}">
    <Label Text ="{Binding UserName}">
  </StackLayout>
</ContentPage>

In the example, the StackLayout is a view just like the custom view that you are using . A good way to see the binding context of the view in the view model is by check OnBindingContextChanged method and see .