Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a C# function through Javascript onClick event in WebView in Xamarin.Forms

I have a post type WebView which I managed to bind with the service response as string but I have some links like related posts which have their ids. On clicking those links I want the user to go to that article. I have tried many solutions but its look like the JavaScript doesn't calls on click, it calls on load because my complete WebView is treated as string and if I concatenate it, it definitely doesn't remains a script.

Here is my complete WebView code and the screenshot attached is the link which is made in WebView.

I managed to make it work by concatenating the whole response in string. Below is the code which helped me achieve this

String getHtml()
{
    var bodyStyle = "<!DOCTYPE html><html><head><style>body{height: 100%;}p{text-align:left;color:#191919;}filter{background-color:#191919;}a:link"
        + "{color:#2588B0; background-color:transparent}a:visited {color:#0099CC; background-color:transparent}"
        + "a:hover{color:#0099CC; background-color:transparent}a:ative{color:#0099CC; background-color:transparent}span{background-color: yellow;color: black}customRight{float: right}</style></head><body>";}

    var refs = bodyStyle;

    refs = refs + "<center><h4>" + response.Title + "<h4></center>";

    if (response.Pc.Count > 0)
    {
        refs = refs + "<center><u>" + Pc +
            "</a></u></center><br>";
    }

    if (string.IsNullOrWhiteSpace(response.Cn))
    {
        refs = refs + "<center>" + response.Cn + "</center><br>";

    }

    if (response.Judges.Count() > 0)
    {
        refs = refs + "<center> <strong>Coram:</strong> " + JudgesN + "</center><br>";
    }

    if (string.IsNullOrWhiteSpace(response.JGD.ToString()))
    {
        refs = refs + "<center> <strong>Decided On:</strong> " + response.JGD + "</center><br>";
    }

    if(string.IsNullOrWhiteSpace(response.AppealType) )
    {
        refs = refs + "<center> <strong>Appeal Type: </strong>" + response.AppealType + "</center><br>";
    }

    if (response.Appellants != null)
    {
        refs = refs + "<left><b>" + apeal + "</b></left>";
        refs = refs + "<customRight>apeal</customRight><br>";

    }
    refs = refs + "<center>VERSUS</center>";

    if (response.Respondants != null)
    {
        refs = refs + "<left><b>" + resp + "</b></left>";
        refs = refs + "<customRight>resps</customRight><br><br>";
    }

    if (string.IsNullOrWhiteSpace(response.FinalVr))
    {
        refs = refs + "<center> <strong>Final:</strong> " + response.FinalVr + "</center><br>";
    }

    if (string.IsNullOrWhiteSpace(response.note))
    {
        refs = refs + "<p> <strong>Note:</strong><br/> " + response.Headnote.ToLowerInvariant() + "</p><br>";
    }

    if(response.Refs != null)
    {
        refs = refs + "<left><b>Refered Jdgmts: </b></left><br>";
        foreach(var obj in response.Refs) 
            {
            if(obj.Jid == null) {
                refs = refs + "<p style='font-size:13px;'>"
                    + obj.Title
                    +"</p>";
            }
            else
            {
                refs = refs + "<p style='font-size:13px;'>" + "<a href=\""
                    + obj.Jid
                         + "\" target=\"_blank\" onClick=\"(function(e){alert('e is here'); "+ loadJt(obj.Jid);+"return false;})(); return false;\">"
                         + obj.Title
                    + "</a>"
                    + "</p>";
            }

    jdgmt = jdgmt.Replace("^^^", "<br/><br/>");
    jdgmt = jdgmt.Replace("<SPARA>", "<p>");
    jdgmt = jdgmt.Replace("</SPARA>", "</p>");

    refs = refs + jdgmt;
    refs = refs + "</body></html>";

    return refs;
}

data.Source = new HtmlWebViewSource { Html = getHtml(), BaseUrl="http://url.com/", BindingContext = response };

Here is my LoadJt Function

string loadJt(string jjid)
{
    loadJmt(jid);// invokes the constructor and updates my VM too.
    return jid;
}

Below how links are displaying

attached screenshot to clear up things more.

like image 316
Shreeti G Avatar asked Jul 19 '18 19:07

Shreeti G


People also ask

What is calling a function in C?

Calling a Function A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.

Can you call C from C++?

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code. extern "C" void f(int); // one way.

What is meant by calling a function?

A function call is an expression that passes control and arguments (if any) to a function and has the form: expression (expression-listopt) where expression is a function name or evaluates to a function address and expression-list is a list of expressions (separated by commas).

Can I call ac function from python?

We can call a C function from Python program using the ctypes module.


1 Answers

Unfortunately when we talk about WebView on mobile. Call a C# function through JavaScript it's a bad way to follow. Because of webview, it's very limited to do that and a lot of problems that you probably will have on each platform too

So here we go a solution that works fine for me and I used it a lot

1) You won't need to implement a CustomRenderer, just on your xamarin.forms and HTML

2) This solution works to Call camera, download files in a native way, and anything else you want, just intercept the navigating event

3) On your HTML implement in your tag

 "<a href=></a>" 

something like this:

"<a href='..MyOnclickMethod?objId="+obj.Jid+"'>"
+ obj.Title+ "</a>"

4) On your Xamarin WebView

...
webview.Navigating+=WebView_Navigating
...


private void WebView_Navigating(object sender, WebNavigatingEventArgs evt)
{
      string url = evt.Url;

      if (url.Contains("..MyOnclickMethod"))
      {
         //do your code here...
            evt.Cancel = true;
      }
}

make sure that you calling evt.Cancel = true; To prevent the webview continuing process requisition

If the id not coming yet: The string that you mounted maybe is not correct If you sure about this and it not working yet, try the pass in

"<a href>" 

a valid URL with your id like

"<a href='www.google.com/?objId='"+obj.Jid+">"

I hope I have helped you

like image 111
7 revs, 3 users 94% Avatar answered Sep 22 '22 15:09

7 revs, 3 users 94%