2015-12-17 98 views
4

我正在研究一個小的Xamarin.Forms webview應用程序。這是以前回答的一個後續問題,xamarin-forms-making-webview-go-backXamarin Forms - Webview沒有顯示

所以我有一個工具欄和一個後退按鈕的實現和工作。但是當我用已經打開的模擬器(即時使用Genymotion)運行程序時,該程序將運行並顯示工具欄以及後退按鈕...但是不會顯示webview。

但是繼承人的奇怪的事情,有時當我運行程序,當模擬器處於睡眠模式,然後切換回程序完美工作。另外,當我在iOS上測試它時,它只顯示了工具欄,並且根本沒有webview!更經常的是,模擬器不會顯示webView。我也在我的Android設備上測試了這一點,並且發生了相同的事情,它會顯示工具欄而不是webview。

提升混淆我知道,但任何人都可以幫助我與此。

我會在下方附上我的代碼:

App.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Xamarin.Forms; 

namespace WebView_form 
{ 
    public class App : Application 
    { 
     public App() 
     { 
      //const string URL = "http://www.google.com"; 
      MainPage = new NavigationPage(new WebPage()); 
     } 

     protected override void OnStart() 
     { 
      // Handle when your app starts 
     } 

     protected override void OnSleep() 
     { 
      // Handle when your app sleeps 
     } 

     protected override void OnResume() 
     { 
      // Handle when your app resumes 
     } 
    } 
} 

WebPage.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection.Emit; 
using System.Text; 

using Xamarin.Forms; 

namespace WebView_form 
{ 
    public class WebPage : ContentPage 
    { 
     private WebView webView; 

     public WebPage() 
     { 
      webView = new WebView 
      { 
       Source = "https://www.google.com" 
      }; 


      // toolbar 
      ToolbarItems.Add(new ToolbarItem("Back", null,() => 
       { 
        webView.GoBack(); 
       })); 

      Content = new StackLayout 
      { 
       Children = { webView } 
      }; 
     } 
    } 
} 

如果有人能幫助我,這將會是大。

回答

11

VerticalOptions設置爲FillAndExpand,如果不起作用,則爲HorizontalOptions執行相同操作。

可能WebView獲得零大小的高度,因爲當佈局發生時視圖仍然是空的。

因此,像這樣改變WebPage.cs中的代碼;

// ... Other code 

public WebPage() 
{ 
    webView = new WebView 
    { 
    Source = "https://www.google.com", 
    VerticalOptions = LayoutOptions.FillAndExpand, 
    HorizontalOptions = LayoutOptions.FillAndExpand 
    }; 


    // toolbar 
    ToolbarItems.Add(new ToolbarItem("Back", null,() => 
    { 
    webView.GoBack(); 
    })); 

    Content = new StackLayout 
    { 
    Children = { webView } 
    }; 
} 

// ... Other code 
+1

工作!謝謝:) – Duggan

0

另一個要考慮的:如果響應WebView.Navigating事件,一定要其args.Cancel未設置爲true,如果加載的頁面是WevView.Source。 WebView.Navigating的iOS實現在加載WebView.Source時觸發,但Android實現沒有。如果在有問題的頁面是WebView.Source時將args.Cancel設置爲true,則WebView將爲空。

+0

你能解釋一下你的想法嗎? –