我的WebView显示了很多未使用的区域,如图所示,Xamarin c#

gr8qqesn  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(150)

我已经在Droid项目中实现了一个自定义的网页视图渲染器。问题是,在我的网页视图中显示了很多未使用的空间。请看图片。

XAML部件

<ContentPage.Content>
       
        <StackLayout >
            <ScrollView>
                <StackLayout>
                    <StackLayout >
                        <Label Text="Be Happy" TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Label Text="This article is about the online encyclopedia." TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Button Text="Test" HorizontalOptions="Center" VerticalOptions="Start"  />
                        <Label Text="Test" TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Label Text="This article is about the online encyclopedia." TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Button Text="Wikipedia" HorizontalOptions="Center" VerticalOptions="Start"  />
                        <Label Text="Test" TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Label Text="This article is about the online encyclopedia." TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Button Text="Wikipedia" HorizontalOptions="Center" VerticalOptions="Start"  />
                        <Label Text="Test" TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Label Text="This article is about the online encyclopedia." TextColor="Red" HorizontalOptions="Center" VerticalOptions="Start" />
                        <Button Text="Test" HorizontalOptions="Center" VerticalOptions="Start"  />
                    </StackLayout>
                                     
                    <StackLayout x:Name="stackview"  BackgroundColor="Red"  />

               </StackLayout>              
            </ScrollView>                              
        </StackLayout>
      
    </ContentPage.Content>

代码隐藏部件

public partial class WebViewTest : ContentPage
 {
        
        string url = "https://en.wikipedia.org/wiki/Wikipedia";
        public WebViewTest( )
        {
            InitializeComponent();

            var browser = new MyWebView
            {
                Source = url,
            };
          
            if (url == "")
            {
                browser.IsVisible = false;             
            }
            else
            {
               
                stackview.Children.Add(browser);
            }

        }
}

对于自定义渲染器,我使用以下代码自定义渲染器代码

guicsvcw

guicsvcw1#

如果你只是想让webview修复你布局中的空白空间,你可以只得到空间的高度,并将其设置为webview的HeightRequest
比如说:

protected override void OnAppearing()
    { 
        browser.HeightRequest = (belowcontrol.Y - abovecontrol.Y);
        base.OnAppearing();
    }

相关问题