I work on a Xamarin.Forms.Shell
app containing 4 tabs. One of these tabs contains a WebView
allowing users to plane an appointment through an URL given by a provider.
The AppShell.xaml
looks like this (all the icons are set with FontImageSource
):
<TabBar x:Name="appTabBar"> <ShellContent x:Name="tabBarHome" Title="Home" Route="HomePage" ContentTemplate="{DataTemplate local:HomePage}" /> <ShellContent Title="News" ContentTemplate="{DataTemplate local:NewsPage}" /> <ShellContent Title="Booking" Route="BookingPage" ContentTemplate="{DataTemplate local:BookingPage}" > <ShellContent.Icon> <FontImageSource Glyph="{StaticResource FalIconCalendar}" FontFamily="FontAwesomeLight" /> </ShellContent.Icon> </ShellContent> <ShellContent Title="About" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" /> </TabBar>
The BookingPage
looks like this:
<ContentPage.Content> <Grid> <ctrl:CustomWebView x:Name="webView" Source="{Binding UrlBooking}" WidthRequest="1000" HeightRequest="1000" IsVisible="{Binding ShowWebView}"> </Grid </ContentPage.Content>
When the BookingPage
is displayed the first time, the WebView
is loading as expected. However, if the user changes tab and returns to the BookingPage
there are 2 behaviors depending to the platform:
WebView
as it was before to changing tab.WebView
is reloaded.I would like to know if there is a way to prevent the WebView
reload on Android?
I've already searched on similar topics like https://forums.xamarin.com/discussion/176470/how-to-prevent-webview-reload-after-the-tab-bar-selection but there was no clear answer.
Answers
You can Bind the webview source OnAppearing() only. Then source will bind when user opens the screen.
This doesn't change anything: the WebView is still reloaded on Android.
When switching to another tab in
Shell
, the page's OnDisappearing will be called. If you return to the previous page, the OnAppearing method is trigerred, which is the reason to make the webView be reloaded.Try creating a custom WebView and make the class a singleon. Then use the singleon webView to test the function.
I'm not sure that it's possible. If I try to do that, I get an exception when I try to display the WebView.
I tested the singleon webview class in a shell application, the issue still occurs in Android. I am looking for the cause, and will update it here later.
How did you did to integrate a singleton WebView class? I've tried but I get an exception, as no public constructor was found...
I'm not sure that there is a solution for this issue that has already been reported on Github.
I add the views in code behind.
The user didn't give a feedback to question, you could report a new issue to the product team on github.