2
votes

I have created a HybridWebView according to https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/ and created a native ios WKWebView. Now I want to add custom controls to go Back/Forward/Home in Forms but I am not sure what is the recommended way to make a call from my Xamarin forms view into my native view to for example trigger the GoBack() of the ios WKWebView browser?

Should I try to call into my native implementation or should the native view listen to events from the forms view?

1
You would probably need to use the Xamarin MessagingCenter functionalities. It follows the publish-subscribe pattern. What you can do is publish a message in your Xamarin PCL and from your native projects you will have to subscribe to these published messages that comes from a Xamarin PCL trigger/event.ADimaano
Thank you, I’ll try that!Mårten
Finally got around to testing this and it works great, post your comment as an answer @ADimaano and I'll mark it as selected answer.Mårten
@MarkErickson in my ios project in my HybirdWebViewRenderer constructor i have placed my subscriptions public HybridWebViewRenderer() { MessagingCenter.Subscribe<HybridWebView>(this, MessageTypes.BrowserBackButtonClick.ToString(), GoBack); } And my methods look like this: private void GoBack(HybridWebView sender) { if (Control.CanGoBack) Control.GoBack(); } ...sorry for the code formatting here, can't seem to get these snippets to look nice.Mårten
Perfect, I forgot about the Control in the ViewRenderer, that's where I couldn't figure out how to get the instance. Thanks!!Mark Erickson

1 Answers

2
votes

You would need to use the Xamarin MessagingCenter functionalities. It follows the publish-subscribe pattern. What you can do is publish a message in your Xamarin PCL and from your native projects you will have to subscribe to these published messages that comes from a Xamarin PCL trigger/event.

Sample code:

In your PCL you will have something like this:

 MessagingCenter.Subscribe<MainPage> (this, "Hi", (sender) => {
     // do something whenever the "Hi" message is sent
 });

And in your native project something like this:

 MessagingCenter.Send<MainPage> (this, "Hi");