Search code examples
xamarin.formsxamarin.ios

Dynamically updated Xamarin.Forms ContentPage Content doesn't respond to user actions for iOS platform


In my Xamarin.Forms project which targets both iOS and Android platforms, I'm trying to change the Content of a ContentPage dynamically in the following way,

var currentPage = Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
var oldContent = ((ContentPage)currentPage).Content;
var testLabel = new Label { Text = "Hello" };
var newContent = new Grid { Children = {oldContent, testLabel } };
((ContentPage)currentPage).Content = newContent;

This would display the testLabel on top of the oldContent successfully.

In Android platform, I can still interact with child elements (Buttons, ListView... etc) in the oldContent view. But in iOS the oldContent view child elements are not-interactive at this stage.

I have also made sure that the testLabel doesn't block the oldContent. This is how I made sure it doesn't block,

var currentPage = Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();
var oldContent = ((ContentPage)currentPage).Content;
var newContent = new Grid { Children = { oldContent } };
((ContentPage)currentPage).Content = newContent;

for this setup too, it's only for the Android platform that I can interact with oldContent child elements. But for iOS platform I cannot interact with oldContent child elements.

Also when I set the oldContent back as the Content of the ContentPage, then I can interact with the oldContent elements. In the same application flow.

((ContentPage)currentPage).Content = oldContent;

This allows me to interact with oldContent child elements for both Android and iOS platforms.

Have anyone got any idea how I can make the oldContent still interactive for iOS platform after dynamically changing the Content of the ContentPage.


Solution

  • I just managed to achieve it,

    I tried creating the newContent as an empty Element. Then replace the Content of the ContentPage with the newContent. Then add the relevant child elements into the newContent like below.

    var newContent = new Grid ();
    ((ContentPage)currentPage).Content = newContent;
    newContent.Children.Add(oldContent);
    newContent.Children.Add(testLabel);
    

    and bingo!!! it worked 😇