如何从SetWebViewClient调用返回?

时间:2012-06-16 00:51:30

标签: c# xamarin.android

我有一个简单的monodroid问题

活动A调用活动B

活动B有一个webview,看起来像这样

SetContentView(Resource.Layout.webView);
WebView webView = FindViewById<WebView>(Resource.Id.webView1);
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl(url);
webView.SetWebViewClient(new dealWithWebView());

在dealWithWebView()中,我有这个

class dealWithWebView : WebViewClient
{
    WebView webView;

    public override void OnPageFinished(WebView view, string url)
    {
        webView = view;
        Context c = webView.Context;
        // it goes away and does something
        // calls another method in the class which returns back here
    }
}

问题是我似乎无法让它从WebViewClient返回,然后从“B”返回“A”。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以执行类似

的操作
SetContentView(Resource.Layout.webView);
WebView webView = FindViewById<WebView>(Resource.Id.webView1);
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl(url);
webView.SetWebViewClient(new dealWithWebView(this));

使用:

class dealWithWebView : WebViewClient
{
    Activity parent;
    public dealWithWebView(Activity parent)
    {
        this.parent = parent;
    }

    public override void OnPageFinished(WebView view, string url)
    {
        var webView = view;
        Context c = webView.Context;
        // it goes away and does something
        // calls another method in the class which returns back here
        // all done - so finish the parent now
        parent.Finish();
    }
}

答案 1 :(得分:0)

您也可以使用:

class WebViewClientCallback : WebViewClient
{
    public event EventHandler PageLoaded = delegate { };

    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);
        PageLoaded(this, EventArgs.Empty);
    }

}

用法:

clientCallback = new WebViewClientCallback();
clientCallback.PageLoaded += delegate(object sender, EventArgs e) {
    // What to do next?
    // Do the things you want to do after PageIsLoaded       
};
webview.SetWebViewClient(clientCallback);
webview.LoadData("<p>HTML-CONTENT</p>", "text/html; charset=UTF-8", null);