2015-12-17 100 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

你能解释一下你的想法吗? –