2012-05-07 35 views
1

我一直在嘗試在使用monodroid的應用程序使用的WebView中顯示進度條實現。我已經達到很遠,但似乎無法解決這個難題的最後部分。我正在使用Monodroid Pro的付費版本,並使用Galaxy S2作爲測試設備。如何在MonoDroid上的WebView中顯示進度條

這是我迄今所做的: -

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實例可以在OnCreate方法進行,而在MonoDroid的一個全新的類將被進行,然後webchromeclient要被第一繼承然後等等。我錯過了什麼?還有另一種我不知道的方式嗎?一些幫助將非常感激。

回答

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

我有一個更完整的瀏覽器演示available here這也可以幫助你走了。

+0

非常感謝您的詳細幫助!!,這真的很有幫助!您提供的指南鏈接也將非常有幫助。在此之後,我將專注於意圖,即接收來自瀏覽器的鏈接,發送給另一個活動等。已經完成了意圖過濾器事件,處理部分仍然存在。再次,非常感謝! – wjbjnr