1

我一直在尝试在WebView使用 monodroid 的应用程序中显示进度条实现。我已经走了很远,但似乎无法解决难题的最后一部分。我正在使用 Monodroid Pro 的付费版本,并使用 Galaxy S2 作为测试设备。

这是我到目前为止所做的:-

在本OnCreate节中:-

        Window.RequestFeature(WindowFeatures.Progress);

        SetContentView(Resource.Layout.Main);

        Window.SetFeatureInt(WindowFeatures.Progress, Window.ProgressVisibilityOn);

        wv.SetWebViewClient(new monitor());

        wv.LoadUrl("https://www.google.com");

现在在 onprogress 上更改了覆盖方法:-

  private class progress : WebChromeClient
  {
        public override void OnProgressChanged(WebView view, int newProgress)
        {                    
           base.OnProgressChanged(view, newProgress);
        }
  }

现在我看到的解决方案是用于 Android 的 java 实现,这很容易,即:-

webview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress)   
    {
        //Make the bar disappear after URL is loaded, and changes string to Loading...
        MyActivity.setTitle("Loading...");
        MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

        //Return the app name after finish loading
        if(progress == 100)
            MyActivity.setTitle(R.string.app_name);
     }
 });

但是使用 monodroid 我不能SetProgress像在 Android 实现中那样使用方法,Activity可以在OnCreateMethod 中创建实例,而在 Monodroid 中,要创建一个全新的类,然后webchromeclient首先要继承,然后以此类推。我错过了什么?还有其他我不知道的方法吗?一些帮助将不胜感激。

4

1 回答 1

2

正如您所注意到的,C# 不支持像 Java 这样的匿名类,因此您需要定义一个单独的类。该Activity.SetProgress()方法是公共的,这意味着您可以将您的活动的引用传递给该类,并使用它来调用该方法:

public class CustomWebChromeClient : WebChromeClient
{
    private Activity _context;

    public CustomWebChromeClient(Activity context)
    {
        _context = context;
    }

    public override void OnProgressChanged(WebView view, int newProgress)
    {
        base.OnProgressChanged(view, newProgress);

        _context.SetProgress(newProgress * 100);
    }
}

然后你的活动可以创建这个类的一个实例,将自己传递给构造函数:

webview.SetWebChromeClient(new CustomWebChromeClient(this));

我在这里有一个更完整的浏览器演示,它也可以帮助你开始。

于 2012-05-07T11:59:45.817 回答