-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
iOS PageRenderer compatibility not rendering page content #7174
Comments
@agendamatic can you try porting your code to a PageHandler? The PageHandler has a And if you want to supply your own custom view you can do so via Just return a view that inherits from the expected type |
I spent some hours trying to work with PageHandler, but I was unable to access the current NavigationItem. I decided to wait for documentation/examples. In the meantime I tried to use the old renderer approach. That resulted in the discovery that PageRenderer is not working as expected. Thus I filed a bug report to point out that issue. Compatibility measures should work correctly. |
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, EventArgs e)
{
#if IOS
if (Handler is IPlatformViewHandler platformViewHandler)
{
var cv = platformViewHandler.ViewController;
var navItem = cv.NavigationItem;
System.Diagnostics.Debug.WriteLine($"{navItem}");
}
#endif
}
} |
Please let us know if that helps anything! |
Hi @agendamatic. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
First, there no longer seems to be a Loaded event on ContentPage. I wired up the HandlerChanged event, and placed the following code in it:
Place a ToolbarButton in the ContentPage.ToolbarButtons collection:
In App.xaml.cs, ensure you have this:
At runtime, the ViewControlller's NavigationController is null, and the NavigationItem contains null for the title. The NavigationItem RightBarButtonItem property is null. This is why I tried to fall back on the old PageRenderer technique (I had concluded that I did not yet understand PageHandler well enough). Within the PageRenderer I can access the NavigationItem as expected, However there appears to be a but in PageRenderer that prevents page content from being rendered. Thus this issue was filed. For the PageRenderer issue. |
I just realized that I had placed the title on the NavigationPage instead of the ContentPage. With that change, the NavigationItem correctly contains the title, however the RightBarButtonItem (and corresponding array) aren't populated. |
I'm using a Xamarin library that inherits the iOS PageRenderer and overrides ViewDidLoad/ViewDidUnload to perform some work. The new rendering scheme for Maui doesn't inherit UIViewController. How can code like this be migrated to Maui? In the mean time I'm using the obsolete PageRenderer to port this code over. |
I would like to see an answer to @cwaldron 's question too pls, we have an iOS renderer that overrides ViewWillAppear to set some toolbar items to the left instead of right, but I don't see how that might work with handlers. In the example from @PureWeen above, the navItem RightBarButtonItems and LeftBarButtonItems are empty, so either that Loaded event is too early for what we need and they haven't been populated yet, or we are now looking in the wrong place. |
The key is that you need to cast the PageHandler as IPlatformViewHandler to access the ViewController, as @PureWeen said. To get to the UINavigationItem, you need to look at the ParentViewController at just the right moment in the page's lifecycle. Unfortunately that moment exists somewhere between the page Appearing event and the Loaded event. I have been able to approximate the functionality of overriding ViewWillAppear by using the following handler. In my apps I use a base class for each page called CorePage:
Note that I say approximate, because there can be visual artifacts when manipulating the buttons during the ContentPage.Loaded event. My implementation replaces generated UIBarButtonItem objects with new ones using native UIBarButtonSystemItem and SF Symbols (this is iOS after all). I overcame the timing issue with a hack: I save/remove the ToolbarItem objects in ConnectHandler and add manually created UIBarButtonItem objects during the Loaded event. This approach works, but I'm not a fan. For the PageRenderer functionality to ultimately be obsoleted, MAUI needs to implement access to the UIViewController during ViewWillAppear in a manner that not only we Xamarin Forms developers are accustomed to, but that is also easily discoverable by new programmers coming to .NET MAUI. |
@agendamatic ah thank you! The key I was missing is the reference to the ParentViewController, that gave me access to the right bar button items and then my XF renderer code could be moved across. I have this on a single page for now, will move it to a registered page handler later today. EDIT: Haven't seen any artefacts yet but will keep my eyes peeled, thank you again! |
Hi, I need to override some methods of the |
So what you'd do here is override CreatePlatformView and copy our code. This should work for you internal class WorkaroundPageHandler : PageHandler
{
class MyPageViewController : PageViewController
{
public MyPageViewController(IView page, IMauiContext mauiContext) : base(page, mauiContext)
{
}
}
protected override Microsoft.Maui.Platform.ContentView CreatePlatformView()
{
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a LayoutView");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} cannot be null");
if (ViewController == null)
ViewController = new MyPageViewController(VirtualView, MauiContext);
if (ViewController is PageViewController pc && pc.CurrentPlatformView is Microsoft.Maui.Platform.ContentView pv)
return pv;
if (ViewController.View is Microsoft.Maui.Platform.ContentView cv)
return cv;
throw new InvalidOperationException($"PageViewController.View must be a {nameof(Microsoft.Maui.Platform.ContentView)}");
}
} |
That was exactly what I've been looking for! Thanks! |
I want to override the navigation bar back button behaviour using below code public class BaseContentPage : ContentPage
builder.ConfigureMauiHandlers( handlers => { handlers.AddHandler(typeof(BaseContentPage), typeof(BasePageHandler)); }); public class BasePageHandler : PageHandler
with the above code the page back button behavior is working as expected. But when I have clicked on the back of the navigation bar of previous page(Content Page), getting Unhandled Exception: Please help me to resolve this issue. |
in android after tab not rendering too |
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process. |
I changed my custom render from XF to a Handler for Maui. To put a searchbar in a navigation bar on iOS. Looked at to the code above. After a lot of error and trial i come up with this:
And the Handler :
In several pages i use this handler And in Mauiprogram.cs: The searchbar is showing in the navigation bar of iOS. (ContentPage A) Everything is working. I can set the placeholder text, or a default text.
and my functions/commands initialized in my contentpage code are fired
The problem is:
What did i wrong? How can i fix this? @PureWeen Any idea how to create a handler specific working for each page? |
Description
In my MAUI iOS app, I need access to the UIViewController to access the NavigationItem. I ported my Xamarin Forms PageRenderer to my app and added it to the MauiAppBuilder using the AddCompatibilityRenderer() method.
At runtime, the renderer is instantiated and the ViewWillAppear overload executes, there is no page content rendered.
Steps to Reproduce
Create a new MAUI app. In App.xaml.cs modify as follows:
Modify MauiProgram.cs as follows:
Run it on an iOS simulator. There is no content rendered.
Comment out the AddCompatibilityRenderer statement and the content is rendered.
Version with bug
Release Candidate 3 (current)
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
iOS 154
Did you find any workaround?
No. I cannot access the NavigationController from the PageHandler either, so I'm pretty much dead in the water.
Relevant log output
No response
The text was updated successfully, but these errors were encountered: