从 Html webview Xamarin 中的按钮触发事件 iOS

Trigger event from Html Button in webview Xamarin iOS

我正在努力触发我的 WebView 中的 html Link Button 事件。我找不到解决办法。我正在做的是:

public void ViewDidLoad(bool animation)
{
    var yourWebView = new UIWebView(new CGRect("YourViewFrame"));
    string htmlString = "<html>Hey!  try this, I'm sure It will work, Now,  <a href='Home.html'>ClickMe</a> Go for it...</html>";
    yourWebView.LoadHtmlString(htmlString, null);
}

我尝试了 Javascript 和 webhybrid,但这并不能满足我的要求。我怎样才能让点击我点击?

如果您希望从 Webview 中获取事件,那么您必须使用 UIWebViewDelegate 在那里您将识别已触发的事件。看看下面的代码片段。

public void ViewDidLoad(bool animation)
{
    var yourWebView = new UIWebView(new CGRect("YourViewFrame"));
    yourWebView.Delegate =new YourWebViewDelegate(this);
    string htmlString = "<html>Hey!  try this, I'm sure It will work, Now,  <a href='Home.html'>ClickMe</a> Go for it...</html>";
    yourWebView.LoadHtmlString(htmlString, null);
}

创建委托 class 来处理您的点击操作。 //魔法

public class YourWebViewDelegate : UIWebViewDelegate
{
    UIViewController CurrentInstance;

    public YourWebViewDelegate(UIViewController _currentInstance)
    {
        CurrentInstance = _currentInstance;
    }

    public override bool ShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
    {
        if (navigationType == UIWebViewNavigationType.LinkClicked)
        {
            CurrentInstance.NavigationController.PushViewController(new YourViewControllerToNavigate(), true);
        }
        return true;
    }
}

干杯!!