来自webview Xamarin iOS中的Html Button的触发事件

时间:2015-10-17 07:16:35

标签: webview xamarin xamarin.ios

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

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,但这不足以满足我的要求。我怎样才能让Clicl Me Hit?

1 个答案:

答案 0 :(得分:1)

如果您希望从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);
}

创建将处理您的点击操作的委托类。 //魔术

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;
    }
}

干杯!!