Xamarin.Forms WebView

Hello, in this post we will talk about WebView with Xamarin.Forms, a WebView is a view that show a WebPage, or more simply HTML content.
We can define the instance of a webview on the next way:

WebView webView= new WebView();
In the property Source we asign the source of our content for our WebView.

Look a few piece of code:

public class WebViewPage : ContentPage
    {
        WebView webView{ get; set;} = new WebView ();
        public WebViewPage()
        {
            Label TitlePage = new Label {
                Text = "Use of WebView",
                FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center,
                TextColor=Color.White
            };
                        
            webView.Source = new UrlWebViewSource{ 
                Url = "https://mx.linkedin.com/in/osvaldo-santiago-estrada-15480741" 
            };
            webView.VerticalOptions = LayoutOptions.FillAndExpand;


            this.Padding = new Thickness (10, Device.OnPlatform (20, 0, 0), 10, 5);

            this.Content = new StackLayout {
                BackgroundColor=Color.FromHex("#2196F3"),
                Children = {
                    TitlePage,
                    webView
                }
            };
        }
    }


We create a new instance of WebView
WebView webView{ get; set;} = new WebView ();

After, we add a Label to show as a Title, and define some attributes:

Label TitlePage = new Label {
                Text = "Use of WebView",
                FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center,
                TextColor=Color.White
            };



After that, we asign the source to webView, also how we want to show in the control:
    webView.Source = new UrlWebViewSource{ 
                Url = "https://mx.linkedin.com/in/osvaldo-santiago-estrada-15480741" 
            };
            webView.VerticalOptions = LayoutOptions.FillAndExpand;



And to finish, add the Controls to the Content of the Page
this.Content = new StackLayout {
                BackgroundColor=Color.FromHex("#2196F3"),
                Children = {
                    TitlePage,
                    webView
                }
            };


Now, look the code running on iOS and Android:







More information in:
https://developer.xamarin.com/guides/cross-platform/xamarin-forms/


Follow me on Twitterhttps://twitter.com/OsvaldoSan
Or find me in Linkedinhttps://mx.linkedin.com/in/osvaldo-santiago-estrada-15480741




Comentarios

Entradas populares de este blog

Entry Show/Hide Password on Xamarin.Forms

Xamarin.Forms Frame

Xamarin.Forms Picker