androidiosxamarin.formsdevexpressxamarin.forms.shell

Xamarin Forms - Xaml Element injected to every page


I am looking for a way to have an element (DXPopup) injected to every page while using Xamarin Forms Shell.

My initial thought would be to create a custom renderer for Tabbar and inject the popup above the tabbar. But I hit a blocker when my tabs are contained in a FlyoutItem. I then thought about a custom renderer for ContentView and add a child element, though hit a blocker where Android wanted an android view.

How would be best to tackle this issue?

Desired outcome: DXpopup element added to every page, so we can call a common show method (by message of mediator class)


Solution

  • I solved this using Jason's suggested route above:

    Basepage - that had a control template Control Template like:

    <ControlTemplate x:Key="BasePageTemplate">
            <StackLayout VerticalOptions="FillAndExpand">
                //your element (in my case a DXPopup
                <ContentPresenter VerticalOptions="FillAndExpand"/>
            </StackLayout>
    </ControlTemplate>
    

    Register to the binding context changing (in code behind of base page): BindingContextChanged += SetBindingContext;

    This was to update to the consuming pages viewmodel.

    Said viewmodel would have a baseViewmodel, where I registered the element injected like so:

    private void SetBindingContext(object sender, EventArgs e)
        {
            if (BindingContext is not ViewModelBase baseVM) return;
    
            var dxPopUp = GetTemplateChild("ElementName");
            baseVM.DxPopUp = dxPopUp as DXPopup;
        }
    

    This in turn allowed me to use the viewmodel base to interact with the element (in my case showing it)