WebView Control in Xamarin.Form is a highly useful control for displaying a Web Page within your Mobile Application. You can either specify the URI that needs to displayed or you can provide the entire HTML Source to be displayed by assigning an instance of UrlWebViewSource to the Source Property of the control.
<WebView Source="{Binding GatewayPageSource,Mode=TwoWay}" WidthRequest="500" HeightRequest="500"></WebView>
For Binding the control to an External URI, you could bind your ViewModel Property as follows.
public HtmlWebViewSource GatewayPageSource { get { return _GateWaySource; } set { SetProperty(ref _GateWaySource, value); } } this.GatewayPageSource.BaseUrl = "urlpath";
For Binding the control to an HTML String, you could bind your ViewModel Property as follows.
this.GatewayPageSource.Html = "HtmlString"
public class AdvancedWebView : WebView { public static readonly BindableProperty NavigatedCommandProperty = BindableProperty.Create(nameof(NavigatedCommand), typeof(ICommand), typeof(AdvancedWebView), null); public AdvancedWebView() { Navigated += (s, e) => { if (NavigatedCommand?.CanExecute(e) ?? false) NavigatedCommand.Execute(e); }; } public ICommand NavigatedCommand { get { return (ICommand)GetValue(NavigatedCommandProperty); } set { SetValue(NavigatedCommandProperty, value); } } }
The XAML with new Control would be as follows
<controls:AdvancedWebView Source="{Binding GatewayPageSource,Mode=TwoWay}" NavigatedCommand="{Binding WebViewNavigatedCommand}" WidthRequest="500" HeightRequest="500"/>
For keeping things simple, I have implemented only Navigated Event in the example, however, we could extend this functionality with Navigating Event as well.
Hi, I am new in Xamarin, can you send the complete example for me.
BR.
LikeLike