D.madushanka
D.madushanka

Reputation: 563

Xamarin forms webview covers every element in xaml page

I have a webview and a label inside one of my xaml page. I am loading a local html file to the webview inside OnAppearing(). Problem is I cannot see the label. Webview takes whole screen. I tested in Android.

    <?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:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="Test.Views.MyWebView">

        <ContentPage.Content>

            <StackLayout BackgroundColor="Red" VerticalOptions="FillAndExpand" Orientation="Vertical">

                <Label HeightRequest="100" TextColor="Black" Text="Sample"/>

                <StackLayout>
                    <ScrollView>

                        <other:HybridWebView x:Name="hybridWebView" VerticalOptions="FillAndExpand" 
                                             Navigating="webView_Navigating" HeightRequest="400" Navigated="MywebView_Navigated" />

                    </ScrollView>
                </StackLayout>

            </StackLayout>


        </ContentPage.Content>
    </ContentPage>

Code behind

OnAppearing

     protected override void OnAppearing()
            {
                base.OnAppearing();
                NavigationPage.SetHasNavigationBar(this, false);


                if (isFirstRun)
                {

                    jsonOutput = JsonConvert.SerializeObject(payData);

                    //var htmlSource = new HtmlWebViewSource();
                    var urlSource = new UrlWebViewSource();

                    string url = DependencyService.Get<IBaseUrl>().Get();

                    TempUrl = Path.Combine(url, "xamarinhtmlmotor.html");
                    urlSource.Url = TempUrl;    

                    hybridWebView.Source = urlSource;
                    isFirstRun = false;
                    Content = hybridWebView;

                }

            }

webView_Navigating

     private void webView_Navigating(object sender, WebNavigatingEventArgs e)
        {
            UserDialogs.Instance.ShowLoading("Loading...", MaskType.Black);
        }

Here I am currently checking for the android devices

MywebView_Navigated

     private async void MywebView_Navigated(object sender, WebNavigatedEventArgs e)
        {

            UserDialogs.Instance.HideLoading();

            if (Device.RuntimePlatform == Device.Android)
            {

                if (e.Url.Equals("file:///android_asset/web/xamarinhtmlmotor.html"))
                {

                    getResult();

                }
                else if (e.Url.Equals("http://localhost/receipt_motor.aspx"))
                {

                    string rtNo = "receiptNo";
                    string cvNo = "<%= hdntxtbxTaksit.ClientID %>";

                    receiptNo = await hybridWebView.EvaluateJavaScriptAsync($"document.getElementById('{rtNo}').value;");
                    cvNoteNo = await hybridWebView.EvaluateJavaScriptAsync($"document.getElementById('{cvNo}');");

                    if (receiptNo != null && !receiptNo.Equals(""))
                    {         

                    }

                    if (cvNoteNo != null && !cvNoteNo.Equals(""))
                    {                  

                    }

                }


            }


            }

        }

Upvotes: 1

Views: 88

Answers (1)

Anand
Anand

Reputation: 1959

Screenshot

Why don't you try with grid?

      <Grid RowSpacing="10">

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Text="Sample" FontSize="Large" HorizontalOptions="CenterAndExpand"></Label>

            <StackLayout Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

                <other:HybridWebView x:Name="hybridWebView" VerticalOptions="FillAndExpand" 
                                             Navigating="webView_Navigating"  Navigated="MywebView_Navigated" />

            </StackLayout>

        </Grid>

Upvotes: 1

Related Questions