Language

ViewHandler.SetContainerView(Object) Method

Definition

Sets or clears the view returned by ContainerView.

protected:
 void SetContainerView(System::Object ^ containerView);
protected void SetContainerView(object? containerView);
member this.SetContainerView : obj -> unit
Protected Sub SetContainerView (containerView As Object)

Parameters

containerView
Object

The platform view that wraps PlatformView, or null to clear the current container view.

Exceptions

Thrown when containerView is not a container type that this handler supports.

Remarks

This is the supported way for a handler that lives in another assembly - for example a platform backend that ships outside of .NET MAUI - to participate in the container view lifecycle. Call it from SetupContainer() after the container has been created and the PlatformView has been re-parented into it, and call it with null from RemoveContainer() after the PlatformView has been moved back to the original parent.

This method only records the container view; it never re-parents views, and it does not change HasContainer. Attaching and detaching the platform views remains the responsibility of the SetupContainer() and RemoveContainer() overrides, which .NET MAUI invokes when HasContainer changes.

A handler in an external backend assembly:
public class MyBackendViewHandler<TVirtualView, TPlatformView> : ViewHandler<TVirtualView, TPlatformView>
    where TVirtualView : class, IView
    where TPlatformView : MyPlatformView
{
    public override bool NeedsContainer =>
        VirtualView?.Background is not null ||
        VirtualView?.Clip is not null ||
        VirtualView?.Shadow is not null ||
        base.NeedsContainer;

    protected override void SetupContainer()
    {
        if (PlatformView is null || ContainerView is not null)
            return;

        var wrapper = new MyWrapperView();
        var parent = PlatformView.Parent;
        parent?.Remove(PlatformView);
        wrapper.Content = PlatformView;
        parent?.Add(wrapper);

        SetContainerView(wrapper);
    }

    protected override void RemoveContainer()
    {
        if (ContainerView is not MyWrapperView wrapper)
        {
            SetContainerView(null);
            return;
        }

        var parent = wrapper.Parent;
        parent?.Remove(wrapper);
        wrapper.Content = null;
        parent?.Add(PlatformView);

        SetContainerView(null);
    }
}

Applies to