Reputation: 2716
When I create WebView in xamarin, it is not visible when the application is launched. It gets visible when the view is readjusted. How can I resolve the above issue.
Here is sample code that I am using
var browser = new WebView();
var htmlSource = new HtmlWebViewSource ();
public SamplePage()
{
InitializeComponent();
StackLayout stackLayout = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children =
{
sometextbox,
browser
}
}
}
//Code within some button's event
htmlSource.Html = @"<html><body>
<h1>Xamarin.Forms</h1>
<p>Welcome to WebView.</p>
</body></html>";
browser.Source = htmlSource;
Upvotes: 0
Views: 610
Reputation: 3460
It seems like the FillAndExpand should be on the webview, not on the StackLayout
Upvotes: 2
Reputation: 11105
The issue is that WebView
has no idea how big it needs to be and so it is probably there but has no size. What I do is to assign a Width
and Height
in SizeChanged
like so:
public SamplePage()
{
InitializeComponent();
StackLayout stackLayout = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children =
{
sometextbox,
browser
}
}
SizeChanged += (sender, args) => {
browser.WidthRequest = Width;
browser.HeightRequest = Height * 0.75;
}
}
If the browser still does not show up, keep the SizeChanged()
code since it will update the WebView
on rotation but also add the same code to OnAppearing()
:
protected override void OnAppearing() {
base.OnAppearing();
browser.WidthRequest = Width;
browser.HeightRequest = Height * 0.75;
}
If you needed to do that upon button click you could do that or you could even leave the above code in place and just change WebView.IsVisible
.
Upvotes: 1