MobileBlazorBindings icon indicating copy to clipboard operation
MobileBlazorBindings copied to clipboard

Expose starting and finished load events in WebView

Open pietrodicaprio opened this issue 4 years ago • 8 comments

Exposing OnNavigationStarting and OnNavigationFinished in razor in order to execute actions on top of the base events.

Disclaimer: the method I used is probably not the best one. Is the first time I code on such kind of project and I'm not sure about how all the stuff is working in behind scenes.

This PR is not intended to be a final approach but mostly a rise of hand about a need I have on this framework. Is then the method is acceptable, well, I'm happier :)

P.S. I didn't create a branch on the latest commit because I can't update my Android project dependencies for it :(

pietrodicaprio avatar Jan 19 '21 15:01 pietrodicaprio

CLA assistant check
All CLA requirements met.

dnfadmin avatar Jan 19 '21 15:01 dnfadmin

Hi @pietrodicaprio thank you for the PR! I think that adding support for these events would be great!

There's a pattern for mapping from Blazor events to Mobile Blazor Bindings / Xamarin.Forms events for registration and also making sure they can get un-registered.

You can see an example of this with the Button's Clicked event:

  • Button component defined the Blazor event: https://github.com/xamarin/MobileBlazorBindings/blob/master/src/Microsoft.MobileBlazorBindings/Elements/Button.cs
  • ButtonHandler uses an event registration API to map it to the Xamarin.Forms event: https://github.com/xamarin/MobileBlazorBindings/blob/master/src/Microsoft.MobileBlazorBindings/Elements/Handlers/ButtonHandler.cs

I think that to fully support the events we should try to follow this pattern.

Would you be able to update the PR with this pattern?

Eilon avatar Jan 20 '21 23:01 Eilon

Hi @Eilon , I saw it but did not completely understood how it is working. I'll give it a try today and eventually push here. Thank you for the hint!

pietrodicaprio avatar Jan 21 '21 07:01 pietrodicaprio

I'm trying to follow the pattern but I'm having a strange issue: while the framework project builds without issue, I'm not able to bind the event as I'm supposed to.

If I want to bind the existing OnWebMessageReceived I have these options: image and I can simply bind a void with a string as argument.

Buf if I want to bind the new OnNavigationFinished I have these options: image and when I bind a new method that has an object and an Uri as argument I have the error: CS1503 Argument 2: cannot convert from 'method group' to 'EventCallback'

I pushed the new code in this PR in order to have, if possible, a review from you

pietrodicaprio avatar Jan 21 '21 09:01 pietrodicaprio

Interesting thing: after closing, reopening, closing, leaving the project away for a couple of hours, opening again, binding a function that has only Uri as parameter (instead of object, Uri) and trying to compile ignoring the error highlighted by Intellisense: it just works 😅

Now the question is: why my VS / Intellisense can't get the updated metadata in order to provide the proper suggestion? Did I made anything wrong in the code?

pietrodicaprio avatar Jan 21 '21 12:01 pietrodicaprio

It looks like you're using a VS extension for Intellisense or another IDE? Is that ReSharper or something else? You could try disabling that and see what the built-in Intellisense shows up. Maybe that's the issue?

Eilon avatar Jan 21 '21 17:01 Eilon

BTW sometimes closing & re-opening is the only fix anyway because of how things get cached. Or even doing git clean -xdf to delete all temporary/generated files in the build.

Eilon avatar Jan 21 '21 17:01 Eilon

I confirm that I have ReSharper. Solved by deleting the .vs folders and executing the git command you provided, thank you for the suggestion!

I confirm that the code works and Intellisense is updated. 🎉

There is only one thing I don't understand: why Xamarin.Forms.WebView calls 2 times the SendNavigated method (that causes a "double call" to the OnNavigationFinished event) at every single page change? Is there a reason behind? I evaluated to put some check in place, in this project, to avoid double calls but then is risky for page refresh, redirects etc.

Said that, if the code matches your expectation I have nothing to add right now. Let me know!

pietrodicaprio avatar Jan 21 '21 20:01 pietrodicaprio