Xamarin Forms - Webview没有出现在我的StackLayout中

时间:2016-06-03 18:56:25

标签: android xamarin webview xamarin.android

WebView显示的原因是什么?我尝试了FillAndExpandCenterAndExpand,但它仍无效!

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:MyApp.Views;assembly=MyApp"
             x:Class="MyApp.Pages.Main">
  <StackLayout>
    <StackLayout Orientation="Horizontal" VerticalOptions="Start">
      <!-- top controls -->
    </StackLayout>

    <StackLayout x:Name="WebViewLayout" VerticalOptions="CenterAndExpand">
      <WebView Source="https://www.google.com/" VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand"/>
    </StackLayout>

    <StackLayout Orientation="Horizontal" VerticalOptions="End">
      <views:AdMobView WidthRequest="400" HeightRequest="50" />
    </StackLayout>
  </StackLayout>


</ContentPage>

3 个答案:

答案 0 :(得分:1)

我建议将WebView从父StackLayout中取出,将最外StackLayout更改为Grid

话虽如此,我认为这可能有助于解决问题而不这样做:

public override void OnAppearing()  {
    SizeChanged += (sender, e) => {
        WebViewItem.WidthRequest = Width;
        WebViewItem.HeightRequest = 200; //Or what ever
    }

    OnSizeChanged(null, null); //Make it run at least once
}

答案 1 :(得分:1)

我不能确切地说出原因,但是在堆栈布局上设置VerticalOptions似乎可以解决这个问题。以下是绘制Web视图的结果:

var webView = new WebView();
webView.Source = "https://www.xamarin.com";
webView.MinimumWidthRequest = 200;
webView.MinimumHeightRequest = 200;
webView.HorizontalOptions = LayoutOptions.FillAndExpand;
webView.VerticalOptions = LayoutOptions.FillAndExpand;

var stackLayout = new StackLayout;
this.Content = stackLayout;
stackLayout.Children.Add(webView);
stackLayout.HorizontalOptions = LayoutOptions.FillAndExpand;
stackLayout.VerticalOptions = LayoutOptions.FillAndExpand;

我承认StackLayout的布局行为令人困惑。

答案 2 :(得分:0)

将StackLayout(用于容纳WebView)的“垂直”选项从CenterAndExpand更改为FillAndExpand和WebView本身。

我希望这会有所帮助。 :)

<StackLayout x:Name="WebViewLayout" VerticalOptions="FillAndExpand">
     <WebView Source="https://www.google.com/" VerticalOptions = "FillAndExpand"/>
</StackLayout>