Skip to content

Add OnCreateContainer API for custom WrapperView support in ViewHandler #30797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 23, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

This PR adds a new OnCreateContainer() virtual method to the ViewHandler base class, allowing developers to provide custom WrapperView implementations for container scenarios. This addresses the need for custom screen reader support and other accessibility scenarios where the default WrapperView is insufficient.

Problem

Previously, developers who needed custom container views had to use reflection to access private setters:

// Before: Required reflection to set custom containers
ContainerView = customWrapperView; // ❌ private setter
((WrapperView)ContainerView).Child = PlatformView; // ❌ internal setter

This was particularly problematic for Windows accessibility scenarios where developers needed to create custom automation peers for controls like Border (which is sealed and cannot be inherited).

Solution

The new API provides a clean, supported way to customize container views:

public class MyCustomViewHandler : ViewHandler<MyView, MyPlatformView>
{
    public override WrapperView OnCreateContainer()
    {
        return new CustomWrapperView(); // ✅ Clean API
    }
}

Changes Made

  1. Added OnCreateContainer() virtual method to ViewHandler base class that returns WrapperView?
  2. Updated ContainerView property from private protected set to protected set for derived class access
  3. Updated all platform implementations (Windows, iOS, Android, Tizen) to call OnCreateContainer() before falling back to default WrapperView
  4. Added comprehensive unit tests covering both custom container and fallback scenarios
  5. Maintained full backward compatibility - existing code continues to work unchanged

Platform Implementation Details

  • Windows: Updated ViewHandlerOfT.Windows.cs to use OnCreateContainer() ?? new WrapperView()
  • iOS: Updated ViewHandlerOfT.iOS.cs to use OnCreateContainer() ?? new WrapperView(bounds)
  • Android: Updated ViewHandlerOfT.Android.cs to use OnCreateContainer() ?? new WrapperView(context)
  • Tizen: Updated ViewHandlerOfT.Tizen.cs to use custom container with fallback

Usage Examples

Basic Custom Container

public override WrapperView OnCreateContainer()
{
    return new AccessibleWrapperView 
    { 
        AccessibilityRole = "CustomControl" 
    };
}

Conditional Custom Container

public override WrapperView OnCreateContainer()
{
    if (VirtualView?.AutomationId?.StartsWith("special_") == true)
    {
        return new CustomWrapperView();
    }
    
    return null; // Use default WrapperView
}

Testing

  • ✅ All existing unit tests pass (11/11 in AbstractViewHandlerTests)
  • ✅ New unit tests verify custom container functionality
  • ✅ New unit tests verify fallback to default behavior
  • ✅ Cross-platform builds successful (Android, .NET Standard)
  • ✅ API compatibility maintained

Fixes #30750.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

- Add OnCreateContainer() virtual method to ViewHandler base class
- Update ContainerView property to protected set for derived class access
- Update all platform-specific SetupContainer implementations to use OnCreateContainer
- Add unit tests to verify custom container functionality
- Maintain backward compatibility with existing container behavior

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] ViewHandler: Provide a way to plug custom WrapperView as a container for the PlatformView Add OnCreateContainer API for custom WrapperView support in ViewHandler Jul 23, 2025
@Copilot Copilot AI requested a review from PureWeen July 23, 2025 18:12
Copilot finished work on behalf of PureWeen July 23, 2025 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ViewHandler: Provide a way to plug custom WrapperView as a container for the PlatformView
2 participants