It's currently not possible to easily replace the platform view for anything that inherits from ContentView
Description
All of our handlers provide a PlatformViewFactory static function that allow users to supply their own view. Ideally users can inherit from our platform view and then just weave in whatever behavior they need to. It's a little tricky for users to be successful here because we use ctor initializers in a lot of cases. This behavior gets lost unless you dive into the source code and in some cases they use initializers that are impossible to reach because they are internal.
https://github.com/dotnet/maui/blob/bcaea570dd469404fce699c9bf38cf8a18a65897/src/Core/src/Platform/iOS/ContentView.cs#L53-L54
Steps to Reproduce
Try to replace the platformview on iOS used for layouts/contentviews
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
public AppDelegate()
{
LayoutHandler.PlatformViewFactory = OnCreateView;
}
private LayoutView OnCreateView(ViewHandler<Microsoft.Maui.ILayout, LayoutView> arg)
{
return new ContentViewTest()
{
CrossPlatformMeasure = arg.VirtualView.CrossPlatformMeasure, // error
CrossPlatformArrange = arg.VirtualView.CrossPlatformArrange, // error
};
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
public class ContentViewTest : LayoutView
{
}
}
Link to public reproduction project repository
N/A
Version with bug
7.0 (current)
Last version that worked well
6.0.312
Affected platforms
iOS
Affected platform versions
iOS
Did you find any workaround?
No response
Relevant log output
No response
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
I also hope it is public, i create a View name ViewPager, its platform view is Pivot(Windows), If i directly add Maui item view to ViewPager's Platform view in Handler, like this:
var platformItemView = mauiItemView.ToHandler(handler.MauiContext).PlatformView;
handler.PlatformView.Items.Add(new PivotItem() { Content = platformItemView };
Maui item view don't have SizeChanged event.
I try copy ContentPanel to my project and do like this:
var platformItemView = mauiItemView.ToHandler(handler.MauiContext).PlatformView;
var contentview= new ContentPanel()
{
CrossPlatformMeasure = (w, h) =>
{
(view as IView).Measure(w, h);
return view.DesiredSize;
},
CrossPlatformArrange = (rect) =>
{
return (view as IView).Arrange(rect);
},
};
contentview.Children.Add(platformItemView);
handler.PlatformView.Items.Add(new PivotItem() { Content = contentview });
Maui item view have SizeChanged event. If i don't use ContentPanel wrap platformItemView, i must create a subclass for Pivot.
I’m beginning to bump into this as I look to modify ToolbarItems behavior to be compatible with a Xamarin.Forms app that made secondary items act like Android (3 dot overflow menu).