Call C# from javascript

Nani_777Nani_777 INMember ✭✭

I need to open a new page in my PCL when a button is clicked in the webview.
I dont know how to do this. someone help me.

Answers

  • pnet1pnet1 USMember ✭✭✭

    Do it solves your issue?

    private async void btnItens_Clicked(object sender, EventArgs e)
     {
          await Navigation.PushModalAsync(new MainPageItens(_idorcamento, lib));
    }
    

    Where MainPageItens is your new Page. In this my case i have two parameters for the constructor of new page.

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    @Nani_777 - There are a few ways of doing this. Probably the easiest is to have the button navigate to a proxy URL, using HTML5/JS. In your Xamarin PCL, add a handler for the Navigating event of the WebView. When that handler is called, have it open the new page.

    For more info, see https://forums.xamarin.com/discussion/72681/what-is-the-easiest-way-to-call-c-method-from-javascript-in-webview#latest

  • Nani_777Nani_777 INMember ✭✭

    @JohnHardman thanks that worked and i have another problem. when I come back by pressing the back button it gives me an error html page because the proxy url doesnot exist. Is there a way to prevent the webview from loading the proxy url

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    @Nani_777 - There's a nice way and a hacky way.

    The nice way is to replace that Navigating event solution by the more complex one at:
    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/

    The hacky way is to do one of a few things. All along the lines of:

    Add a Navigated handler. Check the WebNavigatedEventArgs to see whether navigation succeeded or failed. If it failed, navigate (if necessary) back to the previous URL, either by doing using the GoBack() method, or by navigating explicitly to that URL.

    Obviously, that will fire the Navigating event again, so your Navigating handler will need to differentiate.

Sign In or Register to comment.